- Home /
Why does rigidbodies lag behind when connected to kinematic rigidbodies?
The title says it. When I attach a rigidbody to a kinematic rigidbody with a joint inside a vehicle, it lags behind when I drive the vehicle forwards. Why does it do this?
Here is a picture showing it, the kinematic rigidbody is controllable by a script with the mouse because it's a turret. And the turret itself is a physical rigidbody that is attached to the kinematic rigidbody. But it lags behind when I travel fast. Yet again, why does it do this?
Are you updating the kinematic body in FixedUpdate? If not there can be disparities.
The kinematic Rigidbody is just a gameobject parented to the truck.
It doesn't matter what joint I'm using, you can do and see for yourself. Open up a new project, create a gameobject, make it a rigidbody. Then make 2 new rigidbodies (make them both not use gravity) and parent them as childs inside the main gameobject. Take the first rigidbody and make it kinematic, and then attach the second rigidbody to the kinematic rigidbody with any joint. Drop the main gameobject and see what happens.
It even happens to fixed joints, I have tested it. What is so important about my config?
So, is there any solution to this problem? I'd really like to have my turret to physically react with the environment.
Answer by jcman · Jan 14, 2018 at 08:59 PM
4 years late, but since I just came across this problem I may as well answer!
There are 2 reasons that this can happen as far as I can see:
the kinematic body is being moved in the Update function and not FixedUpdate. Since the Physics system updates after each FixedUpdate, and Update is after Physics, that would cause a one-frame lag.
the kinematic body which has the dynamic body attached to it is itself the child of a rigid body (the trailer in your image). In this case, the kinematic body's position is not updated as part of the physics update because physics does not know about the attachment. Instead the kinematic body's position is updated after physics, but that's too late for it to affect any joints attached to it.
Your problem is likely the second one, and I think that unfortunately the only way to retain the physics connection is to have dynamic bodies all the way through. So, change your kinematic body to a non-kinematic one and attach it to its parent with a FixedJoint.
Note you can set the ConnectedMassScale on a joint to very small if you want to prevent the child object from affecting the motion of the parent. If that sounds the wrong way round it's because ConnectedMassScale is a scale on the inverse mass, so what we are doing is pretending that the connected (parent) object has extremely large mass.