Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Enemies sharing health

$
0
0
I am creating an fps and am up to creating enemies, at the moment i have two for testing. i created a script for the enemy and its health and a shooting script for the player. however all the enemies share the health so if i shoot one, they both lose health. i also have a weapon script which holds the damage they take. how do i fix this? ( i am using C#) **enemy health:** using UnityEngine; public class EnemyHealth : MonoBehaviour { public PlayerWeapon weapon; public int maxHealth = 100; public int currentHealth; public GameObject _player; PlayerShoot shootScript; // Start is called before the first frame update void Start() { currentHealth = maxHealth; shootScript = _player.GetComponent(); } // Update is called once per frame void Update() { if(shootScript._shot == true) { shootScript._shot = false; this.currentHealth -= weapon.damage; } } } **shoot script:** using UnityEngine; public class PlayerShoot : MonoBehaviour { public PlayerWeapon weapon; [SerializeField] private Camera cam; [SerializeField] private LayerMask mask; public bool _shot; void Start() { if (cam == null) { Debug.LogError("PlayerShoot: No camera referenced!"); this.enabled = false; } _shot = false; } void Update() { if (Input.GetButtonDown("Fire1")) { Shoot(); } } void Shoot() { RaycastHit _hit; if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask)) { //We hit something if (_hit.collider.tag == "ENEMY_TAG") { Debug.Log("Hit for " + weapon.damage); _shot = true; } } } } **weapon damage:** using UnityEngine; [System.Serializable] public class PlayerWeapon { public int damage = 10; public float range = 100f; }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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