- Home /
help with enemy health script
Hi I'm very new to unity working on my first project and I've been trying to code a basic enemy health system where if you hit an enemy a certain number of times he dies. Now hit detection works fine, except even though in my scripts the enemy health is set to 10 and the player damage to 1, my enemy consistently dies in two hits instead of 10 and this won't change no matter what I set the numbers to. Where did I mess up?
this is my enemy health script:
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public float Health = 10;
private Animator _animator;
void Start()
{
_animator = GetComponent<Animator>();
}
public void TakeDamage(float damage)
{
if (Health <= 0)
{
return;
}
Health -= damage;
if (Health <= 0)
{
Death();
}
}
private void Death()
{
_animator.SetTrigger("dead");
}
}
and this is my player shooting script:
using UnityEngine;
public class PlayerShootingController : MonoBehaviour
{
// range for hit
public float Range = 100;
public Camera _camera;
public ParticleSystem _particle;
private LayerMask _shootableMask;
void Start()
{
_camera = Camera.main;
_particle = GetComponentInChildren<ParticleSystem> ();
//lock curser to middle of screen for shooting
Cursor.lockState = CursorLockMode.Locked;
//shootable layer assigned to enemies
_shootableMask = LayerMask.GetMask("Shootable");
}
void Update()
{
//if mouse is pressed
if (Input.GetMouseButton(0))
{
//create raycast from camera to mouse position
Ray ray = _camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
//if raycasts hits something on shootable layer
if (Physics.Raycast(ray, out hit, Range, _shootableMask))
{
//print hit to console
print("hit " + hit.collider.gameObject);
//play particle system animation
_particle.Play();
//run enemy health script
EnemyHealth health = hit.collider.GetComponent <EnemyHealth> ();
//have enemy take 1 damage on hit
if (health != null)
{
health.TakeDamage(1);
}
}
}
}
}
can't figure out where I went wrong, any help would be greatly appreciated
Answer by eses · Aug 19, 2018 at 07:10 PM
Hi @Cabri
Seems like you are using Input.GetMouseButton - If you read the manual, you might know the reason.
"Returns whether the given mouse button is held down."
So, this doesn't mean just once, but even if your button press is short, it might have fired your raycast many times.
Use GetMouseButtonDown instead, which only registers in one frame.
You can find more information here:
https://docs.unity3d.com/ScriptReference/Input.html
That fixed my problem, thank you so much! I can't believe that's all it took I've been struggling with this for hours. I've been trying to read the $$anonymous$$anuel but I find it hard to make sense of things without the practical knowledge as this is my first time properly coding anything but I'll try to pay more attention to it. Thank you so much for the quick answer, you saved me a lot of unnecessary work and frustration! :)
You should accept his answer :3 click the little gear icon and choose accept.
Answer by no00ob · Aug 19, 2018 at 07:12 PM
I used to have issues where I had changed a public
variable in the editor and then after that in the code, Unity won't update the variable you have to manually change it in the editor to the desired value. In this case check if the editor has a value of "2" on the enemy health script and if it does change it to "10" in the editor.