- Home /
Question by
Nosmo · Jun 18, 2018 at 05:55 PM ·
enemyontriggerenterenemy healthenemydamageenemy damage
how to kill an emeny?
I want to kill an enemy with a certain number of shots and to give a score afterwards.
I have 2 scripts:
KillMegaloth - kills in 1 shot - gives correct score
public GameObject objectToDestroy;
public GameObject effect;
public int scoreValue = 100;
//public AudioClip DeathAudio;
AudioSource enemyAudio;
void OnTriggerEnter (Collider col)
{
if (col.gameObject.tag == "Bullet")
{
Instantiate (effect, objectToDestroy.transform.position, objectToDestroy.transform.rotation);
ScoreManager.score += scoreValue;
Destroy (objectToDestroy, 1f);
Debug.Log ("Megaloth destroyed");
}
}
EnemyManager - kills in 2 shots - takes away life - gives wrong score - the effect upon death is created 34 times in quick succession
public int MaxHealth = 100;
public int currentHealth;
public int scoreValue;
public GameObject objectToDestroy;
public GameObject effect;
// Use this for initialization
void Start () {
currentHealth = MaxHealth;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
Debug.Log ("Megaloth injured");
}
// Update is called once per frame
void Update () {
if (currentHealth == 0)
{
Instantiate (effect, objectToDestroy.transform.position, objectToDestroy.transform.rotation);
ScoreManager.score += scoreValue;
Destroy (objectToDestroy, 1f); // NB: When the number of frames before destruction is removed, the Megaloth disappears right away and only 1 death fire is created.
Debug.Log ("Megaloth destroyed");
}
}
}
I'm at a loss as to what could be going wrong, any suggestions?
Comment
Best Answer
Answer by MT369MT · Jun 18, 2018 at 06:06 PM
Hi, you set a timer to destroy your enemy so for 1 second the condition currentHealt == 0 will still be true and will repeat increasing the score. Use a bool to know if he is already death.
void Update () {
if (currentHealth == 0 && isDeath == false)
{
Instantiate (effect, objectToDestroy.transform.position, objectToDestroy.transform.rotation);
ScoreManager.score += scoreValue;
Destroy (objectToDestroy, 1f); // NB: When the number of frames before destruction is removed, the Megaloth disappears right away and only 1 death fire is created.
Debug.Log ("Megaloth destroyed");
isDeath = true;
}