Question by
jdistler12 · Aug 13, 2019 at 05:46 AM ·
destroycollider2dattackmelee attacklayercollision
How to identify a single enemy game object?
I need to reference a single game object that is touching my box collider. When my player clicks the mouse and there is an enemy in the attack range, I want the attack animation to trigger and the enemy to be destroyed, but, if there is more than one enemy in this attack range I only want 1 to be destroyed. The code I have works well in identifying when an enemy is in range, but I can't reference the enemy game object to destroy it.
void Update()
{
if (timeBtwAttack <= 0)
{
if (Input.GetMouseButtonDown(0))
{
//you may attack
timeBtwAttack = attackSpeed;
if (attackCollider.IsTouchingLayers(enemies))
{
Debug.Log("I'm touching the enemy");
parentAnimator.SetTrigger("AttackAnimation");
//Delete enemy gameobject
}
else
{
Debug.Log("I'm not touching the enemy");
parentAnimator.SetTrigger("MissAnimation");
}
}
}
else
{
timeBtwAttack -= Time.deltaTime;
}
}
Comment
Your answer
Follow this Question
Related Questions
Detect and destroy a 2D collider if I click with mouse on it 1 Answer
Unity Collider2D is causing the game object to disappear upon collision? 1 Answer
Unity Melee Attack Problem / Atak problemi var yardım edin :( 0 Answers
finding valid targets in range 1 Answer
2D sprite won't destroy on collision 0 Answers