- Home /
OnCollisionEnter not being called despite objects colliding and moving
Still working on a 2D space shooter, and I can't seem to register any OnCollisionEnters. The bullet is able to push enemies and bounce off walls, "is kinematic" is on only for walls, and all 3 elements have box/circle colliders and Rigidbody 2Ds (box for walls and circle for bullets and enemies).
Right now I've got
void OnCollisionEnter(Collision collision)
{
Debug.Log("Bullet Collides");
Destroy(gameObject);
}
The Debug message doesn't appear and the bullet prefab the script is attached to isn't being destroyed.
Answer by Imankit · Nov 08, 2016 at 09:40 AM
You need to use
void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("Bullet Collides");
Destroy(gameObject);
}
OnCollisionEnter works only for 3D colliders and rigidbodies. For Rigidbody2D and 2D Colliders OnCollisionEnter2D is used
It works now! thank you.
Is there anything else I should be wary of when working in 2D? Ive had this problem before with addForce.
Unity has made a completely different physics system for 2D which uses Box2D physics. The 3D physics uses PhysX by nVidia. So everything is different for example for RayCast its Physics2D.RayCast, for Trigger its OnTriggerEnter2D(Collider2D col) and so on. So always look for the 2D parts when working with 2D physics in Unity.
Your answer
Follow this Question
Related Questions
How to get expanding object collide when it touches others? 1 Answer
Collision Enter one of the objects ? 1 Answer
Unity3d Arkanoid platform collision problem 1 Answer
collision enter 1 Answer