- Home /
setting position of two rigidbodies connected by a Fixed Joint
Hello, I am relatively new to Unity, and I am trying to set the position of two rigidbodies that are linked by a Fixed Joint.
Code: rigidbody1.position = spawnPoint.position; rigidbody2.position = spawnPoint2.position;
(spawnPoint and spawnPoint2 are empty game objects set at particular locations)
The physics/animation goes haywire from this unless I remove the Fixed Joint. Even when I just set the position of only one of the rigidbodies, it still does not work. And I can't set the position of a container object for the rigidbodies because the rigidbody children move away from their parent's coordinates during the course of the game I'm making and I need to relocate them to their starting point.
What would be the correct approach here? I appreciate any answers.
Answer by Ashkan_gc · Feb 10, 2010 at 04:07 AM
when you use fixed joints it means that your gameObject should not be able to move so you should remove the joint and move your rigidbodies and then connect the joint again.
It is worth noting that using Destroy to remove the joint, and re-creating the joint in the same frame can result in unexpected behavior (because the destroy doesn't really take effect until the end of the frame).
Luckily setting connectedBody to null, changing the position, and the setting connectedBody back to the original value, does work in one frame.
Answer by duck · Feb 10, 2010 at 10:24 AM
In general you shouldn't adjust the position of physics objects yourself. You should apply forces and allow the physics engine to handle the movement and collisions.
The specific problem you're having is that as far as I know, a "fixed joint" is actually simulated as an extremely rigid and stiff spring.
By moving just one of the objects, you're forcing that spring to be stretched much further than it should be, so during the next physics update the spring exerts a massive force on the two objects resulting in the "haywire" behaviour that you're seeing!
There is a function called "MovePosition" which is designed to allow you to move a rigidbody to a specified position, however it's generally intended for use with kinematic rigidbodies only. You might find that if your objects don't collide with anything during this move, it might work okay, but you'll have to make sure that you move both rigidbodies so that after the move they are still the same fixed distance apart - and so, you won't have introduced a massive force in the fixed joint.
Alternatively you could try disconnecting the joint (set the joint's .connectedBody to null) temporarily before you move the object, then re-attach the joint after the move.
Answer by Ian Spangler 1 · Feb 10, 2010 at 08:14 PM
Thanks, this makes sense. I was aware of the fact that repositioning rigidbodies manually is discouraged, but in this case I actually do need to relocate my character (composed of 2 rigidbodies) to a specified starting point upon a death trigger and doing it on the container object doesn't work because the container object's coordinates don't move along with the rigidbodies' coordinates.
I like the idea of disconnecting the joint dynamically with code. I wasn't sure if that was actually possible, but now that i see it and it looks quite simple, I will give that a try before anything else.
@Ian Please use the add comment
if you want to reply to an answer, rather than posting another answer.
Answer by Santa · May 27, 2011 at 12:39 PM
It is also possible to set gameObject.active = false. Then move objects. And then activate them back.