I am trying to implement an enemy health system where it takes more than one shot to kill an enemy. I’m doing a simple test and trying to do a simple two shots to kill for now but after many bullet shots at the enemy, the enemy is still active. What am I doing wrong?
using UnityEngine;
using System.Collections;
public class EnemyScript : MonoBehaviour
{
private Rigidbody2D enemyRigidbody;
public int health = 100;
private int direction;
public Transform target;
public float speed = 1f;
Vector3 targetDirection;
// Use this for initialization
void Start()
{
enemyRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
enemyRigidbody.freezeRotation = true;
targetDirection = target.position - transform.position;
transform.position += targetDirection * speed * Time.deltaTime;
if (this.gameObject.name == "Bullet")
this.enemyHit(50);
if(health <= 0f)
this.gameObject.SetActive(false);
}
void enemyHit(int damage)
{
health -= damage;
}
}