OnCollisionEnter not called in unity

I checked almost every answer for this, but these were mostly simple errors and errors. My problem is that OnCollisionEnter is not called even when it collides with another rigid body.

here is the part that is not called:

void OnCollisionEnter(UnityEngine.Collision col) { Debug.Log("collision!!!"); foreach(ContactPoint contact in col.contacts) { //checking the individual collisions if(contact.Equals(this.target)) { if(!attacking) { Debug.Log("hitting target"); } else { Debug.Log("dying"); //engage death sequence } } } } 

Even the "collision !!!" A message will appear. I understand that it is misused, or am I forgetting something?

+9
c # unity3d
source share
6 answers

Do you use 2D colliders and solids? If so, use this function instead of OnCollisionEnter

 void OnCollisionEnter2D(Collision2D coll) { Debug.Log(coll.gameObject.tag); } 
+41
source share

You need to make sure that the collision matrix (Edit-> Project Settings-> Physics) does not exclude collisions between the layers to which your objects belong.

Unity Docs

You also need to make sure that another object has: a collider, a rigid body, and that the object itself or any of these components are not disabled.

+1
source share

try it

http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html

 using UnityEngine; using System.Collections; public class Example : MonoBehaviour { void OnCollisionEnter(Collision collision) { foreach (ContactPoint contact in collision.contacts) { Debug.DrawRay(contact.point, contact.normal, Color.white); } if (collision.relativeVelocity.magnitude > 2){ audio.Play(); } } } 
+1
source share

That's what I'm doing:

  • Make sure that the object you want to collide with the target has a non-kinematic hard and mesh collider. My attacker object is a cube and just changes its collider to a mesh collider
  • In the grid collider inspector, make sure you turn on convex. For more details see the grid collider inspector here

Your OnCollisionEnter now works. Hope this helps you.

+1
source share

because you mistakenly specified the parameter class name. it does not make mistakes, also does not work. eg:

 OnCollisionEnter(Collider other) //this is wrong OnCollisionEnter(Collision other) //this is correct 
0
source share

You just need to attach the script to the same object whose need detects a collision.

0
source share

All Articles