The question is answered, right answer was accepted
OnTriggerEnter2D executed once
I have 2 game objects, a player with a Sword and a walking Enemy.
The Enemy has a Rigidbody2D attached to it together with a Box Collider2D(isTrigger). The Sword also has a Box Collider2D(isTrigger) attached to it.
Here is the code that is on the Sword:
void OnTriggerEnter2D(Collider2D other)
{
Enemy enemy;
if (other.gameObject.tag == "Hostile")
{
enemy = other.gameObject.GetComponent<Enemy>();
if (player.attacking)
{
DoDamage(enemy);
}
}
}
When the Sword collides with the Enemy for the first time everything works fine, but when they collide again in the same spot, nothing happens. When the Enemy moves away from its position and the Sword collides again in the new position the code is executed again.
Is there something I'm missing??
Thank you.
enter can only happen when there was a non overlapping state before. looks like your sword never really leaves the enemy trigger.
you can add OnTriggerExit with a Log to check that