applying visual wheels according to position left/right

I have found tutorial on Unity - Manual: Create a vehicle with Wheel Colliders
very useful, but after applying my own custom meshes, I have noticed that they are all rotated I the same direction (facing right, in my case).


Is there a way that I can edit the code given on the link in such way that I can rotate the meshes using two boolean variables, left and right, mutually exclusive to each other, to make them face either to the left or right ?

Since visualWheel is a child of each wheel to have the wheel collider (who really affect the weel behavior) you can change the code visuals whithout affect the physics.

We can do a better performance handling quaternions directly, but since I don’t want to mess the code (and my mind) with quaternions, lets use euler (degrees) angles.

According the instructions of the tutorial, the axis of the weels is X axis, and up is Y, we need to rotate Y axis 180 degrees, but, accordingly with the car orientation. To not reinvent the wheel, we will use transform.Rotate

So, lets change first ApplyLocalPositionToVisuals adding a bool, as you desire, to determine if wheel is right or left, and rotate 180° all the left, on your case.

public void ApplyLocalPositionToVisuals(WheelCollider collider, bool isAtLeft)
    if (collider.transform.childCount == 0) {
    Transform visualWheel = collider.transform.GetChild(0);
    Vector3 position;
    Quaternion rotation;
    collider.GetWorldPose(out position, out rotation);
    visualWheel.transform.position = position;
    visualWheel.transform.rotation = rotation;
    //@CROnerd64 use local forward there, local up didn't seem to work properly
              visualWheel.transform.Rotate(0, 0, 180f, Space.Self);

now you can use it on the FixedUpdate code, just modify the line:

        ApplyLocalPositionToVisuals(axleInfo.leftWheel, true);
        ApplyLocalPositionToVisuals(axleInfo.rightWheel, false);

if found any problem with this code, please comment :slight_smile:

@CROnerd64 nice mars rover btw

EDIT: @CROnerd64 found two errors in the original answer, modifying it to the final script

visualWheel.transform.Rotate (Vector3.up, 180);