- Home /
Rigidbody2D.isKinematic = true and parenting unresponsive
I'm trying to have one object parent to another when they collide (essentially what a fixed joint would do if we had one for 2D). Here is the code I'm using:
collider2D.enabled = false; // just for good measure
rigidbody2D.isKinematic = true; // disable physics simulation on my object
transform.parent = coll.transform; // parent to object we've collided with
coll.rigidbody.angularVelocity = 90.0f; // spin object we collided with to test parenting
Unfortunately this works but with a slight delay where my object remains under physics control for a few frames before the parent relationship takes over. Oddly enough I can get things to work correctly if I just destroy the rigidbody2D component straight away:
collider2D.enabled = false; // just for good measure
DestroyObject(rigidbody2D); // remove rigidbody
transform.parent = coll.transform; // parent to object we've collided with
coll.rigidbody.angularVelocity = 90.0f; // spin object we collided with to test parenting
So what gives? From everything I've read online isKinematic = true should be the equivalent of other components' enable flag. Note: I don't want to destroy and re-add the rigidbody as I will continue to need it after unparenting the objects later in the game. Any ideas?
Edit: After some messing around I've noticed that is specifically the parenting that is being delayed not the isKinematic change. All the other aspects of isKinematic seem to take affect immediately.
Hi, I´m having same problem here. Removing the component and adding again doesn´t look like a good solution. I tried adding some yield statements but is not clear how many frames does it take to stop processing physics. I´ll keep you informed if I find a solution.
Answer by Sebito · Apr 24, 2014 at 02:46 PM
Hey, there is a Joint you can use, DistanceJoint2d with distance = 0. That´s the way I solved it. Hope this helps. You don´t need to parent objects.
Thanks for the suggestion but unfortunately the DistanceJoint2D only ensures a positional relationship between two rigidbodies but leaves each free to rotate independently. I am trying to simulate one object attaching/hugging the other so this wasn't sufficient. I really wish the Unity $$anonymous$$m had made a FixedJoint2D since that is exactly what I need and it already exists in the original physics system.
Then parenting and using the joint should fix your angle problem I think.
Your answer
Follow this Question
Related Questions
How to make ridigbody2D continue physics path after drag 1 Answer
useAutoMass and child objects 1 Answer
How to make physics relative to moving parent 1 Answer
Camera following Rigidbody jitter every few seconds with background 0 Answers
Something with physics happen when client picks up something 0 Answers