Search Unity

How can I make a gear system like in gta games?

Discussion in 'Physics' started by mero6191, Aug 31, 2019.

  1. mero6191

    mero6191

    Joined:
    Dec 29, 2018
    Posts:
    15
    i did this but car feels so toy like i want to add a gear system. I searched on interned but all i found is gear systems that are so complex and like in simulation games others are so simple that is not even a gear system (just speed limiter)
    but i want to add a gear system like in gta (it does shifts automatically but the car is manual and speed is relative to the gears)
    here is part of my code that i want to implement the gear system

    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.         gameObject.GetComponent<Rigidbody>().centerOfMass = CenterOfMass.localPosition;
    4.         foreach (WheelCollider hit in Wheels)
    5.      {
    6.             var Car = gameObject.GetComponent<Rigidbody>();
    7.                 var AllWheel = hit.GetComponent<WheelAbilities>();
    8.                 if (AllWheel.CanDrive && IsEngineOn) //Segment For Driving
    9.                 {
    10.                 if (Input.GetKey(HandbrakeKey))
    11.                 {
    12.                     hit.brakeTorque =  brakePower;
    13.                    
    14.                 }
    15.                 else
    16.                 {
    17.                     hit.brakeTorque = 0;
    18.                 }
    19.                 if (torqueDir >  0)
    20.                 {
    21.                     hit.motorTorque = (torque * torqueDir );
    22.                 }
    23.                 if (torqueDir <  0)
    24.                 {
    25.                     hit.motorTorque = (torque * torqueDir) / 2 ;
    26.                     if(hit.rpm > 10 && gameObject.GetComponent<Rigidbody>().velocity.z > 2  )
    27.                     {
    28.                         hit.brakeTorque = ReverseBrakes;
    29.                     }
    30.                     else
    31.                     {
    32.                         hit.brakeTorque = 0;
    33.                     }
    34.                 }
    35.                 if (torqueDir == 0)
    36.                 {
    37.                     hit.motorTorque = 0;
    38.                 }
    39.                 }
    40.                 if (AllWheel.CanSteer) //Segment for Steering
    41.                 {
    42.                     hit.steerAngle = steerAngle * steerDir;
    43.                 if (Input.GetKey(HandbrakeKey))
    44.                 {
    45.                     hit.brakeTorque = brakePower / 5;
    46.                 }
    47.                 else
    48.                 {
    49.                     hit.brakeTorque = 0;
    50.                 }
    51.             }
    52.              
    53.          
    54.      }
    55.     }
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,074
    It's so much more than speed relative to the gear.

    What you need is Torque and rotation speed of an engine with relation to amount of push on acceleration pedal.

    Torque will give you acceleration working on a car, rotation speed will give you max speed of a car. All this for a given gear.



    But to simplify all this. You can do simple relation the bigger the gear the smaller the acceleration but bigger max speed. Acceleration should depend on amount of push on a pedal.
     
  3. mero6191

    mero6191

    Joined:
    Dec 29, 2018
    Posts:
    15
    thanks for reply i can understand it a bit now but i still can not implement it to my code. :)