- Home /
Enemies sharing health
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<PlayerShoot>();
}
// 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;
}
Answer by toddisarockstar · May 19, 2020 at 05:07 AM
the player script needs to lookup the enemy script when the shot happens. not the other way around !!!!
//We hit something
if (_hit.collider.tag == "ENEMY_TAG")
{
Debug.Log("Hit for " + weapon.damage);
PlayerShoot ps = hit.gameObject.GetComponent<PlayerShoot>();
ps.currentHealth -= ps.weapon.damage;
}
@toddisarockstar im not quite sure how that works. i tried putting that into the shoot function but it looks like you want me to reference the Player Shoot (ps) on the Player Shoot script? or am i suppose to put this into the enemy health script? this is what i have now and there are a few errors: PlayerShoot: using UnityEngine;
public class PlayerShoot : $$anonymous$$onoBehaviour {
public PlayerWeapon weapon;
[SerializeField]
private Camera cam;
[SerializeField]
private Layer$$anonymous$$ask mask;
void Start()
{
if (cam == null)
{
Debug.LogError("PlayerShoot: No camera referenced!");
this.enabled = 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 == "ENE$$anonymous$$Y_TAG")
{
Debug.Log("Hit for " + weapon.damage);
PlayerShoot ps = _hit.gameObject.GetComponent<PlayerShoot>();
ps.currentHealth -= ps.weapon.damage;
}
}
}
}
my inspector says there is something wrong with the gameObject just after the _hit and something wrong with ps.currentHealth.
this is the EnemyHealth script: using UnityEngine;
public class EnemyHealth : $$anonymous$$onoBehaviour {
public int maxHealth = 100;
public int currentHealth;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
}
}
UPDATE: after a bit more research i found a solution and it is thanks to you @toddisarockstar !!!! this is what i did:
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 == "ENE$$anonymous$$Y_TAG")
{
Debug.Log("Hit for " + weapon.damage);
EnemyHealth enemyHealth = _hit.transform.gameObject.GetComponent<EnemyHealth>();
enemyHealth.currentHealth -= weapon.damage;
}
}
}
Answer by SunnyChow · May 19, 2020 at 02:14 AM
in your script, if the player shoot and hit something, shootScript ._shot will be true, and all EnemyHealth will detect (shootScript._shot == true) and reduce hp.
Please make up your simple logic first
@SunnyChow Would i have to go into the shoot script and ins$$anonymous$$d of ENE$$anonymous$$Y_TAG create tags for each enemy then call a different bool each time? that would get really messy and i doubt its the best way to do this
Your answer
Follow this Question
Related Questions
Shooting and damage with raycast doesnt work. 0 Answers
Deal damage on collision 2 Answers
Damage script is screwed up...? what to do? 1 Answer
How do I create projectile weapons that all have travel time? 0 Answers
Enemy wont take Damage. How could i fix this?,Enemy wont take damage with raycast 1 Answer