Quantcast
Channel: Questions in topic: "gun"
Viewing all articles
Browse latest Browse all 592

Bullet not affecting health

$
0
0
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Gun : MonoBehaviour { public Transform firepoint; public GameObject bulletPrefab; void Start() { } // Update is called once per frame void Update() { if (Input.GetButtonDown("Fire1")) { Shoot(); } } void Shoot() { Instantiate(bulletPrefab, firepoint.position, firepoint.rotation); } } using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { public int health = 100; void Start() { } // Update is called once per frame void Update() { } /* void OnTriggerEnter(Collider hit) { if (hit.gameObject.tag == "KillZone") { Destroy(gameObject); } }*/ public void TakeDamage(int damage) { health -= damage; if (health <= 0) { Die(); } } void Die() { Destroy(gameObject); } void OnTriggerEnter(Collider other) { Bullet bullet = other.GetComponent(); if (bullet != null) { TakeDamage(Bullet.damage); } // Destroy(gameObject); } } using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet : MonoBehaviour { public float speed = 20f; public static int damage = 40; public Rigidbody rb; void Start() { } void OnTriggerEnter(Collider hitInfo) { Enemy enemy = hitInfo.GetComponent(); // BossHealth boss = hitInfo.GetComponent(); if (enemy != null) { enemy.TakeDamage(damage); } Destroy(gameObject); /* if (boss != null) { boss.TakeDamage(damage); } Destroy(gameObject); */ } // Update is called once per frame void Update() { transform.Translate(Vector3.forward * speed * Time.deltaTime); Destroy(gameObject, 2); } }

Viewing all articles
Browse latest Browse all 592

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>