- Home /
Child Rigid Body collision problem
Hi, I've run across a weird problem lately which I'm unable to resolve myself.
My goal is to create multiple cars which each consists of 2 objects, front and back.
-The front and back are supposed to detect collision. (but different colliders!)
-The front isn't suppose to be affected by physics, the back is.
-The front is also supposed to come of at a certain point, and then react normally to physics.
-The front part has to move perfectly with the back part until it comes of.
The thing is, I can't figure out how. Solutions I've tried:
If I lock the child rigid body in local space(either with code or with the kinematic check-box), it will stop detecting collision with other child rigid bodies(other car fronts) in the scene.
If I remove the rigid body component but still lock the child into place, collision will look normal, but collision will not be detected.
If I make the front object a child with a rigid body, but without the lock, it will work properly, but obviously react separately to physics when compared to the parent.
Edit:
Two colliders with a rigid body as parent doesn't work either, unless there is some way to differentiate between colliders by code, as both colliders send their data to the parent rigid body(whilst I aim to to have different effects on the back and front).
Two rigid bodies attached by a Fixed Joint almost does the trick, except that joints allow for some degree of movement between the bodies. Also joints require two non kinematic rigid bodies...which means the front will always be affected by physics.. :(
I'm not sure what to do at this point.. I'm thinking its not possible actually, as it would probably require collision of two kinematic rigid bodies..
Any tips or suggestions for alternative routes to the same result would be greatly appreciated!
Answer by fafase · Jun 11, 2013 at 06:27 AM
Add en empty game object and put all of your colliders as children. Now put the rigidbody on the parent object (the empty one you created).
When you want to separate them, just unparent the object:
transform.parent = null;
and it will go on its own.
Problem with this setup is, that it seem to detect all collision on the parent empty game object, ins$$anonymous$$d separately on the front and back..pity because it looked very promising
Thanks for the suggestion though!
Answer by Some_weirdGuy · Oct 06, 2013 at 08:27 AM
(been a few months and you probably have this solved/found other methods, but for any who have a similar question and see this: )
This code will give the desired functionality of detecting child colliders:
void OnCollisionEnter(Collision other){
ContactPoint contact = other.contacts[0];
print(gameObject.name +" got collided from contact " + contact.otherCollider);
}
collision(unfortunately means it won't work with triggers) contains a contacts variable:
http://docs.unity3d.com/Documentation/ScriptReference/Collision-contacts.html
http://docs.unity3d.com/Documentation/ScriptReference/ContactPoint.html
you can then retrieve the first contact( contacts[0] ), and check the 'otherCollider' variable it contains, to get the specific collider that made contact(in this case, the child object's collider).
Your answer
Follow this Question
Related Questions
Realistic collisions with a kinematic rigidbody? 1 Answer
2D motion/collision: physics, or direct translation? 2 Answers
Are my kinematic rigidbodies slowing my scene down? 2 Answers
Preventing objects from intersecting with minimal physics 3 Answers
Disable rigidbody from being able to move other rigidbodies 1 Answer