Search Unity

Question Simple wheel colliders sliding and Flips over befor starting to move

Discussion in 'Physics' started by umen, Sep 5, 2020.

  1. umen

    umen

    Joined:
    Oct 2, 2012
    Posts:
    49
    i have a simple car which i set wheel collider to the wheels and the mesh under (the road) is mash collider
    i set here is my configuration in pictures :
    the car config


    here is the wheel collider config :


    here is the body as box collider :



    and this is the result when i hit play :



    and the script that attached to the car :


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Car : MonoBehaviour
    6. {
    7.     public WheelCollider wheelColliderLeftFront;
    8.     public WheelCollider wheelColliderRightFront;
    9.     public WheelCollider wheelColliderLeftBack;
    10.     public WheelCollider wheelColliderRightBack;
    11.  
    12.     public Transform wheelLeftFront;
    13.     public Transform wheelRightFront;
    14.     public Transform wheelLeftBack;
    15.     public Transform wheelRightBack;
    16.  
    17.  
    18.     public float motorTorque = 100f;
    19.     public float maxSteer = 20f;
    20.     //public Transform centerOfMess;
    21.     //private Rigidbody _rigidbody;
    22.  
    23.  
    24.     void Start()
    25.     {
    26.         //_rigidbody = GetComponent<Rigidbody>();
    27.  
    28.         //_rigidbody.centerOfMass = centerOfMess.localScale;
    29.     }
    30.     void FixedUpdate()
    31.     {
    32.         wheelColliderLeftBack.motorTorque = Input.GetAxis("Vertical") * motorTorque;
    33.         wheelColliderRightBack.motorTorque = Input.GetAxis("Vertical") * motorTorque;
    34.         wheelColliderLeftFront.steerAngle =  Input.GetAxis("Horizontal") * maxSteer;
    35.         wheelColliderRightFront.steerAngle = Input.GetAxis("Horizontal") * maxSteer;
    36.  
    37.     }
    38.  
    39.     private void Update()
    40.     {
    41.         var pos = Vector3.zero;
    42.         var rot = Quaternion.identity;
    43.         wheelColliderLeftFront.GetWorldPose(out pos, out rot);
    44.         wheelLeftFront.position = pos;
    45.         wheelLeftFront.rotation = rot;
    46.  
    47.         wheelColliderRightFront.GetWorldPose(out pos, out rot);
    48.         wheelRightFront.position = pos;
    49.         wheelRightFront.rotation = rot;
    50.  
    51.         wheelColliderLeftBack.GetWorldPose(out pos, out rot);
    52.         wheelLeftBack.position = pos;
    53.         wheelLeftBack.rotation = rot;
    54.  
    55.         wheelColliderRightBack.GetWorldPose(out pos, out rot);
    56.         wheelRightBack.position = pos;
    57.         wheelRightBack.rotation = rot;
    58.     }
    59.  
    60. }
    61.  

    why is that happening?
    how can I fix this? i want to understand what i did wrong?
    Thanks
     
  2. Kofiro

    Kofiro

    Joined:
    Feb 3, 2017
    Posts:
    34
    Why have you commented out the line where the center of mass is set?

    Code (CSharp):
    1. _rigidbody.centerOfMass = centerOfMass.localScale;
    Uncomment from line 20 to 28 and create an empty gameobject as a child of the car gameobject and set that to be the transform centerOfMass

    It's spelt centerOfMess in your code though. Basically you need to set the rigidbody's center of mass because it's needed by the wheel colliders. The wheel colliders act kind of like springs and so without the center of mass being set the springs are sort of pushing the car body up and so ends up tilting your vehicle when you press play.
     
  3. umen

    umen

    Joined:
    Oct 2, 2012
    Posts:
    49
    Thanks
    because with or without them it give the wired result
     
  4. Kofiro

    Kofiro

    Joined:
    Feb 3, 2017
    Posts:
    34
    Try lowering the empty gameobject's transform Y position.

    Edit: If that doesn't work, move the Update method contents to FixedUpdate()
     
  5. umen

    umen

    Joined:
    Oct 2, 2012
    Posts:
    49
    It worked Thanks
     
  6. Kofiro

    Kofiro

    Joined:
    Feb 3, 2017
    Posts:
    34
    You're welcome!