- Home /
Problem is not reproducible or outdated
Which Object Calls OnCollisionEnter()?
When a collision happens, does the object with the rigidbody have to call the OnCollisionEnter, or can either? I'm having a problem where the OnCollisionEnter object is on an object without a rigidbody, and will not call when hitting the Player, which has a rigidbody.
Both objects have box colliders, and neither are marked as triggers.
Edit: This issue no longer happens in newer versions of Unity.
Answer by Eric5h5 · Nov 21, 2011 at 05:34 AM
OnCollisionEnter is called on any object involved in a Collision event.
I thought so. However, the function is only called when I have a rigidbody on the object as well as the player. The rigidbody on the player is not marked as $$anonymous$$inematic, yet it doesn't call the function. Any idea on why this may be happening?
How are you deter$$anonymous$$ing that? If I have this script:
function OnCollisionEnter (other : Collision) {
Debug.Log (other.gameObject.name);
}
and I put it on two objects, a cube (collider only) and a sphere (rigidbody + collider), then have the sphere drop onto the cube, in the output is two lines, "Cube" and "Sphere".
That's the odd thing, it isn't being called on either, unless both objects have a rigidbody. Does the object with the rigidbody need to be the object that moves into the collision? And I've tested it with on one object or the other, and the collision is still not detected.
If objects are static, they should not have rigidbodies. If objects are moving, they should. ($$anonymous$$anually moving objects with colliders but no rigidbodies causes the physics engine to have to recompute collision trees, which is a drain on CPU.) However, a rigidbody will collide just fine with an object that has no rigidbody, and both objects get collision messages, as I described in my previous comment.
Both objects move, only the player moves form side to side while objects move toward them. Something I just discovered, if the object starts close enough to the player that it lags into the player when pressing Play in the editor, the event is called.
Answer by DaveA · Nov 21, 2011 at 03:57 AM
If the OnCollisionEnter function is in a script on the Player, it will fire in that script with the 'collider' parameter indicating the other object. You could try putting this function on the other object and see if both fire, indicating each other.
Answer by Jeff-Kesselman · Nov 16, 2014 at 05:50 PM
The stock Unity player controllers have their own private physics and collision implementations. As mentioned in comments above, they have their own, different callbacks defined.
This is a pain in the butt. I pretty much always build my own player objects instead unless its for a very quick test of something else.
Follow this Question
Related Questions
Collision Only being detected on one of the objects involved in the collision - C# 0 Answers
Why OnCollisionEnter cannot detect collisions between colliders? 1 Answer
Cannot get collision to do anything 1 Answer
simple onCollision and onTrigger problem 1 Answer
OnCollisionEnter not triggering when two rigidbody collide via Instantiate 1 Answer