OnTriggerExit2D firing when the object stops.
Hi,
I have a problem with 2D colliders. I'm trying to learn Unity by making a simple tower defence game. Right now, I have a Turret tile with a CircleCollider2D marked as trigger and a Monster unit with a kinetic RigidBody2D and a PolygonCollider2D. I can move the monster with arrow keys from one tile to another. When I move in range of the turret, I recieve a OnTriggerEnter2D event and the turret starts firing at the monster. However, when the monster stops on a tile in range of the turret, I receive a OnTriggerExit2D event. Is this behavior excepected or am I doing something wrong? If so, Is there any way to get around this? Here is the relevant code:
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag.Equals("Monster"))
{
inRange.Add(other.gameObject);
particles.Play();
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag.Equals("Monster"))
{
inRange.Remove(other.gameObject);
}
}
void Update()
{
if (inRange.Count == 0)
{
particles.Stop();
return;
}
.....................
if (inRange.Count > 0 && !particles.emission.enabled)
{
particles.Play();
}
}
Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612055708im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is there a way to prevent my character colliding with background? 0 Answers
How to create 2D terrain procedurally with 2D physics colliders ? 0 Answers
With two box colliders on one object, how can I tell which one was hit? 1 Answer
Why Object2D jump out collision? 1 Answer
Rectangular sprite with circle collider - camera keeps spinning 0 Answers