Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Wheels rotating wrong way

Discussion in 'Physics' started by seoneizz, Oct 24, 2021.

  1. seoneizz

    seoneizz

    Joined:
    Oct 23, 2021
    Posts:
    3
    Hi! I just made a script from tutorial and now wheels are spinning wrong axis. Please help.

    Here is code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SimpleCarController : MonoBehaviour
    6. {
    7.     public void GetInput()
    8.     {
    9.         m_horizontalInput = Input.GetAxis("Horizontal");
    10.         m_verticalInput = Input.GetAxis("Vertical");
    11.     }
    12.  
    13.     private void Steer()
    14.     {
    15.         m_steeringAngle = maxSteerAngle * m_horizontalInput;
    16.         frontW.steerAngle = m_steeringAngle;
    17.     }
    18.  
    19.     private void Accelerate()
    20.     {
    21.         rearW.motorTorque = m_verticalInput * motorForce;
    22.     }
    23.     private void UpdateWheelPoses()
    24.     {
    25.         UpdateWheelPose(frontW, frontT);
    26.         UpdateWheelPose(rearW, rearT);
    27.     }
    28.     private void UpdateWheelPose(WheelCollider _collider, Transform _transform)
    29.     {
    30.         Vector3 _pos = _transform.position;
    31.         Quaternion _quat = _transform.rotation;
    32.         _collider.GetWorldPose(out _pos, out _quat);
    33.  
    34.         _transform.position = _pos;
    35.         _transform.rotation = _quat;
    36.     }
    37.     private void FixedUpdate()
    38.     {
    39.         GetInput();
    40.         Steer();
    41.         Accelerate();
    42.         UpdateWheelPoses();
    43.     }
    44.    
    45.     private float m_horizontalInput;
    46.     private float m_verticalInput;
    47.     private float m_steeringAngle;
    48.  
    49.     public WheelCollider frontW;
    50.     public WheelCollider rearW;
    51.     public Transform frontT;
    52.     public Transform rearT;
    53.     public float maxSteerAngle = 30;
    54.     public float motorForce = 50;
    55.  
    56. }
    57.  
    Here is video:
     
  2. thecloudkeeper

    thecloudkeeper

    Joined:
    Sep 21, 2021
    Posts:
    28
    Is it possible for you to share your
    WheelCollider.cs
    script as well? That will probably help people be able to tell what's going on.
     
  3. seoneizz

    seoneizz

    Joined:
    Oct 23, 2021
    Posts:
    3
    I dont have script like that. I only have made wheel collider objects
     
  4. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    401
    It looks as though your wheel meshes have been modelled on the wrong axis (for this purpose).

    Ours are the same. What we've ended up doing is having, in the scene:

    WheelCollider_Object
    Thing_We_Position_In_Update_Wheel_Pose
    Actual_Wheel_Mesh_Turned_To_Face_Right_Direction
     
  5. seoneizz

    seoneizz

    Joined:
    Oct 23, 2021
    Posts:
    3
    I don't understand what you said in end. Im very new in unity.
     
  6. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    401
    upload_2021-10-25_14-6-29.png

    Is that what your scene looks like? Is the wheel mesh a separate or child object to the one with the collider on it?
     
  7. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    401
    This is part of our scene. The wheelcollider you can see is on the 'frontright' object.
    It has a child that I've renamed to PassThisToUpdateWheelPose.
    PassThis... has a child Rt_front_tyre, and that's the one with the mesh on it.

    upload_2021-10-25_14-22-44.png