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

Fix vehicle physics?

Discussion in 'Physics' started by jerahya, Jun 1, 2015.

  1. jerahya

    jerahya

    Joined:
    May 21, 2015
    Posts:
    10
    Hi, I'm trying to learn how to make vehicle physics that works fine for both simulation and arcade type game. I found a opensource one and it's demo is perfect for what I wan't to learn from. Thing is the script is written in Unity4.3, when I try it in unity5 it doesn't behave that way that is should. Not even close. Is there a specific documentation on how to fix it? I never used unity4 before so I don't know the physics difference of the two and how to fix it.

    sorry for a bad grammar. :p
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Would you mind posting the script? ;)

    Maybe it is still using gameObject.rigidbody instead of doing gameObject.GetComponent<RigidBody>() and caching it.
     
  3. jerahya

    jerahya

    Joined:
    May 21, 2015
    Posts:
    10
    This is the code. One of the proble
    Code (JavaScript):
    1. #pragma strict
    2. @script AddComponentMenu("Vehicle/CarControl")
    3.  
    4. public var Wheel_FL : WheelCollider;
    5. public var Wheel_FR : WheelCollider;
    6. public var Wheel_RL : WheelCollider;
    7. public var Wheel_RR : WheelCollider;
    8. public var GearRatio : float[];
    9. public var CurrentGear : int = 0;
    10. public var EngineTorque : float = 600.0;
    11. public var MaxEngineRPM : float = 3000.0;
    12. public var MinEngineRPM : float = 1000.0;
    13. public var SteerAngle : float = 10;
    14. public var controlsEnabled : boolean = false;
    15. public var COM : Transform;
    16. public var Speed : float;
    17. public var maxSpeed : float = 150;
    18. public var skidAudio : AudioSource;
    19. private var EngineRPM : float = 0.0;
    20. private var motorInput : float;
    21.  
    22. function Start () {
    23.     rigidbody.centerOfMass = Vector3(COM.localPosition.x * transform.localScale.x, COM.localPosition.y * transform.localScale.y, COM.localPosition.z * transform.localScale.z);
    24. }
    25.  
    26. function Update () {
    27.     Speed = rigidbody.velocity.magnitude * 3.6f;
    28.     rigidbody.drag = rigidbody.velocity.magnitude / 100;
    29.     EngineRPM = (Wheel_FL.rpm + Wheel_FR.rpm)/2 * GearRatio[CurrentGear];
    30.    
    31.     ShiftGears();
    32.    
    33.     //Input For MotorInput.
    34.     motorInput = Input.GetAxis("Vertical");
    35.    
    36.     //Audio
    37.     audio.pitch = Mathf.Abs(EngineRPM / MaxEngineRPM) + 1.0;
    38.     if (audio.pitch > 2.0) {
    39.         audio.pitch = 2.0;
    40.     }
    41.    
    42.     //Is The Car Enabled "Is The Player In The Car"
    43.     //If The Player Is In The Car Do These.
    44.     if(controlsEnabled){
    45.         //Steering
    46.         Wheel_FL.steerAngle = SteerAngle * Input.GetAxis("Horizontal");
    47.         Wheel_FR.steerAngle = SteerAngle * Input.GetAxis("Horizontal");
    48.        
    49.         //Speed Limiter / Engine Input.
    50.         if(Speed > maxSpeed){
    51.             Wheel_FL.motorTorque = 0;
    52.             Wheel_FR.motorTorque = 0;
    53.         }else{
    54.             Wheel_FL.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
    55.             Wheel_FR.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
    56.         }
    57.        
    58.         //HandBrake
    59.         if(Input.GetButtonDown("Jump")){
    60.             Wheel_FL.brakeTorque = 100;
    61.             Wheel_FR.brakeTorque = 100;
    62.         }
    63.         if(Input.GetButtonUp("Jump")){
    64.             Wheel_FL.brakeTorque = 0;
    65.             Wheel_FR.brakeTorque = 0;
    66.         }
    67.     }
    68.    
    69.     if(motorInput <= 0){
    70.         Wheel_RL.brakeTorque = 30;
    71.         Wheel_RR.brakeTorque = 30;
    72.     }else if (motorInput >= 0){
    73.         Wheel_RL.brakeTorque = 0;
    74.         Wheel_RR.brakeTorque = 0;
    75.     }
    76.    
    77.     //SkidAudio.
    78.     var CorrespondingGroundHit : WheelHit;
    79.     Wheel_RR.GetGroundHit( CorrespondingGroundHit );
    80.     if(Mathf.Abs(CorrespondingGroundHit.sidewaysSlip) > 10) {
    81.         skidAudio.enabled = true;
    82.     }else{
    83.         skidAudio.enabled = false;
    84.     }
    85. }
    86.  
    87. function ShiftGears() {
    88.    
    89.     if (EngineRPM >= MaxEngineRPM) {
    90.         var AppropriateGear : int = CurrentGear;
    91.        
    92.         for (var i = 0; i < GearRatio.length; i ++) {
    93.             if(Wheel_FL.rpm * GearRatio[i] < MaxEngineRPM) {
    94.                 AppropriateGear = i;
    95.                 break;
    96.             }
    97.         }
    98.         CurrentGear = AppropriateGear;
    99.     }
    100.    
    101.     if(EngineRPM <= MinEngineRPM) {
    102.         AppropriateGear = CurrentGear;
    103.         for ( var j = GearRatio.length-1; j >= 0; j -- ) {
    104.             if ( Wheel_FL.rpm * GearRatio[j] > MinEngineRPM ) {
    105.                 AppropriateGear = j;
    106.                 break;
    107.             }
    108.         }
    109.         CurrentGear = AppropriateGear;
    110.     }
    111. }
    I tried modifying the whole code but I just can't get it to feel like I'm driving a vehicle.
    You can check the project thread and demo here. http://armedunity.com/topic/6569-vehicle-physics-free-project/
    One of the issue was the rear wheels colliders getting drag rather than roll with the vehicle. Other issue I have with it are front wheels acting funny like jittering a bit when idle.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    The wheel physics system was completely rewritten from Unity 4 to 5. One consequence of this is that code written for the old system probably won't work for the new one.
     
  5. jerahya

    jerahya

    Joined:
    May 21, 2015
    Posts:
    10
    Which makes it hard for me to make a proper car physics. :<
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384