Matching rotation of wheels to the movement of a vehicle.

So I made a custom vehicle physics that moves a vehicle without using wheel colliders, well I don’t use wheels at all, and it works perfectly, but now I want to add wheels and match their rotation to the movement of a vehicle. Well, I did calculations for rotation on the X axis, Y axis, and Z axis just needs to be frozen to the rotation of the Z axis of a vehicle. If I rotate them separately, they do work as supposed, but together I am having trouble. so the code looks like this:

transform.rotation = Quaternion.Euler(transform.eulerAngles.x + deltaRotX,
                                        car.transform.eulerAngles.y + rotY,
                                        transform.eulerAngles.z);

Well, the way I do it is, to calculate how much additive rotation it requires on the X axis(in angles), and how much rotation it should have on the Y axis(in angles, depending on the Y rotation of the car), and I just want rotation on the Z axis to stay as it is…
It doesn’t work at all, it is just going crazy on a flat surface but going up/down hills is just a disaster…

This isn’t really a physics question. Your problem stems from your assumption that transform.eulerAngles is controlling rotational orientation and progresses in a linear way.

What is actually happening is that rotational orientation is stored in Unity as a quaternion and you can request an equivalent Euler angle orientation of this quaternion. Unfortunately, there are many possible Euler angle values for any one quaternion orientation:

Imagine a bicycle wheel suspended in space (x forward, y up, z sideways) with no initial rotations. The valve for the tyre is front most (positive x). When you next see the wheel the valve is rear most (negative x). You may say the Euler angles to achieve this are (0, 0, 180) because you think it has rotated about the axle 180 degrees. An equally valid Euler rotation would be (180, 180, 0) a rotation about the forward axis followed by a rotation about the vertical axis.

Unity offers no guarantees to give you something consistent with the change that you made to the Euler angles previously - it takes your requested change, rotates the transform and saves it as a new quaternion orientation. You can then request another valid set of Euler angles for this quaternion and modify them again, but they may not bear any numerical similarity to the previous ones.

Although you are not changing the z rotation, you should be aware that those Euler angle rotations are applied in this sequence: Z, X, Y, which is often unexpected (Unity - Scripting API: Transform.eulerAngles).

When you take car.transform.eulerAngles.y + rotY and put it into [transform.eulerAngles.x + deltaRotX, car.transform.eulerAngles.y + rotY, transform.eulerAngles.z] you are assuming that the car transform and wheel transform are following some sort of consistent set of Euler angles, which they won’t be.

You need to work out the angular change that you want to make as a quaternion and then multiply the original quaternion by it (for quaternions multiplying has the effect of adding the second orientation to the first). You can do this sequentially for rotation and steering if you wish. You almost certainly do not need to start constructing quaternions from scratch, do some research around this and you will find lots of topics on how people rotate objects.

If the idea of working with quaternions is too painful then the other option is to keep a vector3 of Euler angles at the start (0,0,0), modify it, save it and set your Quaternion.Euler value from it without ever trying to subsequently extract new Euler values from the transform.

1 Like

This was really helpful sir, thanks a lot!

1 Like