- Home /
Question by
Magmaw · Dec 26, 2019 at 09:57 PM ·
rigidbodycollidertriggerrigidbody2d
Rigidbody2D alternating between TriggerEnter and TriggerExit?
I have a rigidbody2d with a script attached that has OnTriggerEnter2D and OnTriggerExit2D. They each print a statement when the rigidbody enters or exits a trigger, but for some reason, it switches between them every frame and I don't know why.
Here's a picture of the circle collider touching the ground. [1]: /storage/temp/150505-capture.png
Here is the script:
void OnTriggerEnter2D(Collider2D collision)
{
print("enter");
}
void OnTriggerExit2D(Collider2D collision)
{
print("exit");
}
capture.png
(20.4 kB)
Comment
Answer by xibanya · Dec 30, 2019 at 08:15 PM
Check the tag of the object that's causing the collision, and only take an action if it's the sort of object you're looking for. Example:
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player") Debug.Log("enter");
}