Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Rotation not consistent

Discussion in 'Physics' started by JackSinko, Feb 29, 2024.

  1. JackSinko

    JackSinko

    Joined:
    Nov 29, 2019
    Posts:
    5
    Hi! I have a car with wheel colliders and wheel meshes as separate gameobjects.

    Each fixed update, I set the mesh transform to match the wheel collider position and rotation.

    Code (CSharp):
    1. void FixedUpdate()
    2. {
    3.     for (int count = 0; count < allWheelMeshes.Length; count++)
    4.     {
    5.         Transform mesh = allWheelMeshes[count];
    6.         WheelCollider wheelCollider = allWheelColliders[count];
    7.        
    8.         wheelCollider.GetWorldPose(out Vector3 wheelPosition, out Quaternion wheelRotation);
    9.         mesh.position = wheelPosition;
    10.         mesh.rotation = wheelRotation;
    11.     }
    12. }
    However, I want to set the local Y rotation of the mesh transform to 0 afterward so that it points straight and doesn't rotate due to the steeringAngle on the wheelCollider. Simply doing
    mesh.localRotation = Quaternion.Euler(mesh.localEulerAngles.x, 0f, mesh.localEulerAngles.z);
    after setting the mesh rotation as the wheel rotation can sometimes show a local Y rotation of 180 instead of 0 as the wheel rotates, which makes the wheel rotate in the wrong direction. I would expect the rotation to always be 0. Why is this?

    Example - see local Y position of mesh transform going from 0 to 180 and the direction changing.
     
  2. JackSinko

    JackSinko

    Joined:
    Nov 29, 2019
    Posts:
    5
    Edit: resolved by having a separate gameobject to pivot the wheel versus the normal rotation