Answer the question
In order to leave comments, you need to log in
Destroy multiple enemies instead of one?
Hello, what am I doing wrong? There are 2 identical enemies on the stage, a script is placed on each, and if, for example, 4 hp is taken away from the first one, and then you start hitting the other one, the first one will disappear by itself when I hit the second one 4 times. If you completely kill the first, and go to the second, then the second will also have 8 hp. What is the problem?
using UnityEngine;
public class EnemyDeath : MonoBehaviour
{
public int enemyHealth = 8;
private bool hitByPlayer;
void Update()
{
if (hitByPlayer && Knight.punch) // Knight.punch - Проверка, если игрок нажал на кнопку удара
{
goblinHealth -= 1;
if (goblinHealth <= 0)
{
Destroy(gameObject);
}
}
}
private void OnCollisionEnter(Collision enemy)
{
if (enemy.gameObject.CompareTag("Player"))
{
hitByPlayer = true;
}
else
{
hitByPlayer = false;
}
}
}
Answer the question
In order to leave comments, you need to log in
OnCollisionEnter worked, causing damage to the first character. He left the first character, but he still has hitByPlayer == true. You attack the second character. Damage is dealt to both players at once.
Apparently, it is necessary to call in OnCollisionExit
hitByPlayer = false;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question