OnCollisionEnter not working (42397)

I’m having some issues with a certain use case when two objects touch on screen, but do not “collide”.

The scenario is, that the bottom of the screen has shapes. When i click on the shape and drag, it generates a new shape and follows the mouse cursor, while following the mouse, the rigidbody is set to kinematic. When the shape is let go, i turn on gravity to make it drop to the bottom of the screen hitting a floor surface. When it reaches resting point, i turn it back to kinematic so that it stays in that position.

The problem arises when i create another object and try to drag it into the object on the floor. For some reason it doesn’t detect collisions between these two objects. However. If i drag two objects simultaneously and make them touch their OnCollisionEnter scripts get fired.

Is there a limitation i am overlooking? Can two kinematic objects not collide?

Cheers

3 Answers

3

make sure you’re spelling it right and case sensitive (OnCollisionEnter, OnTriggerEnter), spelling it wrong will not cause an error so you won’t know.

Thank you for that comment :D I was going crazy with this problem and noticed that I wrote "onCollisionEnter" instead of "OnCollisionEnter". Thank you thank you thank you!

–

thanks for this. lol i have been programming off and on since 1986 and i still managed to mess this up. So used to python, C++, etc, where method names almost never start with capital letters.

–

Thank you this helped <3

–

From http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html “Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.” You could maybe use OnTriggerEnter instead.

Excellent. Thanks for that, i must have overlooked that line =/ I'll try a combination of triggers and colliders to see if i can get the desired effect.

–

Alright, so that does allow me to test two kinematic objects if they collide. But then the issue comes where you cannot test between a kinematic and a gravity based object anymore, because one now has to be a trigger to test against kinematic. It all gets too confusing and complex for what it needs to be. Your answer did provide enough info for my question however =] Unfortunately it is not the optimal solution for the problem i'm trying to solve.

–

Can't you do somthing like: While draging: IsTrigger = On, Kinematic = 0n. While falling: IsTrigger = Off, Kinematic = Off. Else: IsTrigger = Off, Kinematic = On. And then use OnTriggerEnter while it's dragged and OnCollisionEnter for every other state?

–

@hathol This doesn't seem to be the case in 4.1. I have two gameobjects that are colliding (one can't pass through the other), and one has a non-kinematic rigidbody. Despite all of that, OnCollisionEnter is not called.

–

void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag != “Enemy” && col.isTrigger != true) { Destroy(gameObject); } }

you have to write Collision2D not Collider2D … :slight_smile:

obviously it through error because this is collision not trigger, you wants trigger then write this void OnTriggerEnter2D(Collider2D col) { if (col.gameObject.tag != "Enemy" && col.isTrigger != true) { Destroy(gameObject); } } https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html

–