OnTriggerEnter2D not called after collision (2D game)
I'm brand new to Unity and I'm having issues with my first collision detection. My moving object is a ball and has Rigidbody2D and CircleCollider2D. My stationary "collider or trigger" is a BoxCollider and has a script attached. The OnTriggerEnter2D should be fired when the ball passes through the stationary box. I've also tried OnCollisionEnter2D but I'm fairly certain I should use OnTriggerEnter2D because my stationary box is marked as a trigger.
My code:
public class LoseCollider : MonoBehaviour {
public LevelManager levelManager;
void OnCollisionEnter2D(Collision2D collision)
{
print("collide");
levelManager.LoadLevel("Lose");
}
void OnTriggerEnter2D(Collider2D trigger)
{
print("trigger");
levelManager.LoadLevel("Lose");
}
}
And screenshots of my 2 object properties:
Ball:
Collider:
I made sure to keep them on the same Z plane, so I'm fairly certain the objects ARE colliding. I just must be missing something simple in my properties or something. Any help is appreciated.
Answer by Adriannax · Apr 29, 2017 at 06:45 PM
Hi, I'm new to unity also, try the following code in your boxscript 8,-))
void Update () {
}
public void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("we are in the trigger");
}
public void OnTriggerExit2D(Collider2D collision)
{
Debug.Log("we have left the trigger");
}
}
make sure the ball is set as not a trigger and that the box with the script is set as a trigger, I hope this Is of some help too you, have fun 8,-))
Matt.
Answer by saltysquid · Apr 29, 2017 at 07:43 PM
Hey thanks. I just figured it out and it seems I added a "Box Collider" to my object instead of a "Box Collider 2D". Once I swapped it out everything worked as expected. :)
Thanks
Your answer
Follow this Question
Related Questions
How do I get collision and trigger to work? 2 Answers
Problems with using OnTriggerStay2d 0 Answers
Bounds.Intersects returns false while collision still works? 1 Answer
Detect the objects staying on top of the Particles 0 Answers
2D: Destroy object with dynamic collider after exiting object with static collider 1 Answer