Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collision detection not working in Unity 2D

Tags:

c#

unity3d

I have two 2D game objects. They each have a Box Collider 2D and a Rigid Body 2D which is not kinematic. When the game plays, one moves towards the other and collides with it.

However, I also have the following method in the moving GameObject:

void OnCollisionEnter(Collision collision) 
{
    print( "Collided with someone" );
}

The print statement never prints, so presumably the method is never called. Where am I going wrong?

like image 914
Jean Finley Avatar asked Dec 02 '13 17:12

Jean Finley


People also ask

Why is my collider not working?

When it seems a collider is not working in your Unity scene, there are a few things that you might want to look at. Check if the colliders corresponding to the game objects are attached to their respective game objects. If one or both colliders are not attached, that's the first thing you should fix.


1 Answers

Unity has replicated all of the physics methods for 2D with the word "2D" stuck onto the end! So for your example, it should be changed to:

void OnCollisionEnter2D(Collision2D collision)

And the same with basically any other 2D physics thing.

like image 96
T. Kiley Avatar answered Sep 20 '22 17:09

T. Kiley