Always stay upright and follow parent's y and z rotation

How do I force a child object to always stay upright and follow the parent’s y and z rotation?

I’ve created an object that resembles to this:

alt text

I’m using rigidbody on the parent with two child objects containing wheel collider in each. I was able to move it and turn. All okay, but I just wanted the main body to stay up-right. When moving and hitting rocks on ground, I need the whole object to freely rotate in any direction as it is driven by physics. When the object is flipped upside down, the main body is to rotate back to its upright position.
I added this to the parent:

void Update()
{
    transform.Rotate(new Vector3(0.5f, 0, 0));
}

I added this on a child:

void Update()
{
    transform.LookAt(Vector3.up);
}

But that causes the child to always face up and cannot rotate in x and z axis when the parent rotates in all axis.

To ensure the child follows the parent’s y and z axis, I tried this:

void Update()
{
    transform.LookAt(Vector3.up);
    transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, 0, 0);
}

When you start the game, the child rotates, but wait until the parent finishes its revolution, then the child will remain facing upright. Another full revolution of parent, the child then rotates.

I’ve included an [45667-spintest.zip|45667] to demonstrate what I’m trying to do.

How do I force a child object to always stay upright and follow the parent’s y and z rotation?

EDIT:
After reading Eno Khaon’s answer, I realize that the attached demo I provided was not clear. I’ve updated the file to explain the demo better.

[45698-twowheelsobject.zip|45698].

2 Answers

2

under the rigid body, lock the rotation

I need the whole object to freely rotate in any direction as it is driven by physics. Let's suppose I threw a grenade at it. Boom. If I had the rigidbody freeze rotation in any axis, the object wouldn't move in a natural motion while flying off the ground from an explosion. I want it to be able to flip upside down. Once it is upside down, the main body would also be upside down. Freezing the rotation does not rotate the main body back to its upright position and is not the solution I am looking for.

Maybe I’m looking at this the wrong way, but it sounds like you should try applying torque.

Using AddTorque on the parent should give you a means of flipping back upright if flipped over, then you can scale the strength of the torque applied relative to the dot product of its transform.up and the global Vector3.up values.

I've used motorTorque to move and rotate the wheels. Works good. The physics isn't an issue and flipping isn't either. The two-wheel object should flip in any direction and do not require flipping back. My original question is how do I force a child object to always stay upright and follow the parent's y and z rotation? Take a look at the attached file in the original post to see what my problem is.

Yes, that's related to my original question: How do I force a child object to always stay upright and follow the parent's y and z rotation? The same goes for the two wheel colliders, how do I force the wheel colliders to always face downward and follow parent's y and z rotation? Am I going to always have [Gimbal Lock][1] problem and should abandon all hope? [1]: http://en.wikipedia.org/wiki/Gimbal_lock

I've tried various quaternion solutions to no avail. The solution you gave me doesn't make the child follow the parent's y and z axis at all. It looks like I'll have to come up with really different solution. I'm not giving up. Is there anyone else out there that can solve this problem?