- Home /
Need second pair of eyes: rigidbodies with colliders not firing OnCollisionEnter yet again...
It's gotta be something with the parent/child relationships:
Nothing fancy here:
GameObject 1:
-Capsule collider with Is Trigger DISABLED. I want physics reactions.
-Rigidbody with Is Kinematic DISABLED. I want physics reactions.
GameObject 2:
-Rigidbody with Is Kinematic DISABLED. I want physics reactions.
ColliderGameObject:
-Capsule collider with Is Trigger DISABLED. I want physics reactions.
-Script
AnotherColliderGameObject:
-Box Collider
(This is mentioned to demonstrate to you that this is a compound physics object.)
(No script desired.)
GameObject 3:
GameObjectContainer:
ModelGameObject:
ModelGameObject:
-Box Collider with Is Trigger DISABLED. I'm not doing anything with triggers.
-Script
Another ModelGameObject:
-Box Collider with Is Trigger DISABLED. I'm not doing anything with triggers.
-Script
A bunch more ModelGameObjects with box colliders but without scripts.
GameObject 1 -> GameObject 2 = FAIL. No collision detected. Why!?
GameObject 1 -> GameObject 3 = SUCCESS. Collision detected, even though there is only one rigidbody between the two objects, and it isn't even on the one with the script!
The difference is that GameObject 2 has a rigidbody where as GameObject 3 does not. If I were a betting man, I would have presumed 1 -> 2 would have worked over 1 -> 3. Hence why I need a second pair of eyes.
Nothing fancy with the script either:
void OnCollisionEnter(Collision collision)
{
Debug.Log("hit it!");
}
Riddle me that, Batman. Thanks.
(Googling is just leading me to a bunch of cases of people not knowing how to use Unity's physics system. This is some fringe case and I've been looking at the same situation for too long.)
Answer by tanoshimi · Oct 19, 2014 at 06:28 AM
"Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached." (http://docs.unity3d.com/ScriptReference/Rigidbody.OnCollisionEnter.html). So that's the second mystery solved: only one object needs a rigidbody attached, and the collision event is sent to both objects involved in the collision. If either implements OnCollisionEnter then they can act upon it.
As for the first mystery, if I understand your hierarchy correctly, you need to move the script onto the parent object:
GameObject 2:
-Rigidbody with Is Kinematic DISABLED. I want physics reactions.
-Script
ColliderGameObject:
-Capsule collider with Is Trigger DISABLED. I want physics reactions.
etc.
"Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached." << I wasn't sure if that meant anywhere in the hierarchy chain or literally side-by-side. After posting the question I ended up working on other on-hit type stuff with compound objects and noticed that the script would work if it was side-by-side with the rigidbody. I guess side-by-side it is! Thanks for being my second pair of eyes.
A shame you can't put the scripts on the collider you want to specifically test though, for simplicity's sake. I suppose I will have to dig through the Collision's Collider property to figure out if the right thing was hit.
Your answer
Follow this Question
Related Questions
Applying proper drag and center of mass for a vehicle 1 Answer
When and when not to use kinematic rigidbody? 2 Answers
OnTriggerEnter - Objects pass through each other? 3 Answers
Is it possible to tell Unity physics to ignore a collision at collision time? 1 Answer
Raycaste vs trigger Colliders & Rigidbody performance 1 Answer