OnTriggerEnter not being called
I have an empty object on the "Default" layer. It has a BoxCollider2D component with IsTrigger set. I tried with and without a static RigidBody component. It has this script attached, but none of the logs showed up in the console:
public class TriggerAction : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
Debug.Log("Trigger entered!");
}
void OnTriggerStay(Collider other)
{
Debug.Log("Trigger still inside!");
}
void OnTriggerExit(Collider other)
{
Debug.Log("Trigger left!");
}
}
I also have a player character on the "Default" layer. It has a Rigidbody2D component with a Dynamic body type and a BoxCollider2D that does not have IsTrigger set. To be safe, I added a call to OnTriggerEnter
to its script with a similar debug log. I've tried moving the player both by setting the velocity of its RigidBody2D and calling AddForce
.
If I understand https://docs.unity3d.com/Manual/CollidersOverview.html correctly, the trigger object is a "Static trigger collider" and the player is a "Rigidbody collider", which should interact with each other. I've checked that the default layer collides with itself (I added other static rigidbodies with non-trigger colliders to the level and confirmed the player runs into them only if the default-default checkbox is set).
I've checked multiple questions and none of the answers helped. All of the answered questions suggest one of the fixes I've already tried. An earlier project I worked on has similar trigger code which seems to work there.
I'm completely stumped; if anybody has any suggestions I'd love to hear them!
Answer by Antifinidictor · Oct 07, 2021 at 02:25 AM
It turns out there are 2D versions of OnTriggerEnter
:
public class TriggerAction : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Trigger entered!");
}
void OnTriggerStay2D(Collider2D other)
{
Debug.Log("Trigger still inside!");
}
void OnTriggerExit2D(Collider2D other)
{
Debug.Log("Trigger left!");
}
}
Your answer
Follow this Question
Related Questions
Unity 2D does not detect collisions 0 Answers
Collision check 0 Answers
Collision With Text 0 Answers
Collider Issue 0 Answers
Absolutely no collision detection in build but editor is fine 1 Answer