- Home /
OnCollisionEnter not working
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
Answer by hathol · May 15, 2012 at 11:53 PM
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, $$anonymous$$inematic = 0n.
While falling: IsTrigger = Off, $$anonymous$$inematic = Off.
Else: IsTrigger = Off, $$anonymous$$inematic = 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.
Answer by kfirmon · Sep 26, 2015 at 05:58 PM
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" ins$$anonymous$$d of "OnCollisionEnter". Thank you thank you thank you!
happy it helped :) I was going through the same thing myself...
thanks for this. lol i have been program$$anonymous$$g 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.
you're most welcome :) happy to help. have a great weekend my friend :)
Answer by Soniya_unity · Jul 21, 2017 at 08:09 AM
void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag != "Enemy" && col.isTrigger != true) { Destroy(gameObject); } }
you have to write Collision2D not Collider2D .. :)
https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
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/$$anonymous$$onoBehaviour.OnTriggerEnter2D.html