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

motorTorque keeps on being applied

Discussion in 'Scripting' started by mads232, Dec 1, 2015.

  1. mads232

    mads232

    Joined:
    Sep 6, 2013
    Posts:
    48
    [FIXED] See below post for answer.

    Hello Unity Forum!

    So I got this script which is supposed to control my vehicle, which got 3 gears. Forward, neutral and backwards.

    Although everytime I shift gear, it does not stop applying the motorTorque.

    Here's what I've found out:
    • Nothing to do with input (My axis's). (My old script works just as it should with Axis)
    • Gear script work as it should
    • As soon as I remove all these if conditions, it works as it should.

    This is my gear shifting script. (See spoiler)

    This is alright. Just posting it for better understanding.

    Code (csharp):
    1.  
    2.     void changeGear(int gear)
    3.     {
    4.         //currentGear = 2 is forward 1 is neutral 0 is back
    5.  
    6.         if (gear == 1)
    7.         {
    8.             if(currentGear != 2)
    9.             {
    10.                 currentGear += 1;
    11.  
    12.                 leftStick.transform.RotateAround(leftStickPivot.transform.position, leftStick.transform.right, 10);
    13.  
    14.             }
    15.         }
    16.         else if (gear == 0)
    17.         {
    18.             if(currentGear != 0)
    19.             {
    20.                 currentGear -= 1;
    21.  
    22.                 leftStick.transform.RotateAround(leftStickPivot.transform.position, leftStick.transform.right, -10);
    23.             }
    24.         }
    25.     }
    26.  


    This is my primary motorscript. (See spoiler)

    As you can see, it should only drive forward when gear is in 2. (Or backwards when in 0)

    At first when I start the game and the gear is set to 1 (which is neutral), it seems to work. As soon as I kick it in 2, it starts to drive forward slowly (As it should. idleMotor applies some torque to make it go slowly forward.)

    But if I then shift it back to 1 (neutral) it just doesn't stop applying that torque from idleMotor. It keeps going forward. The same happens in the reverse gear, or if I speed it up with VerticalVehicle Axis.
    Code (csharp):
    1.  
    2.  
    3.         float idleMotor = maxIdleMotorTorque;
    4.         float motor = maxMotorTorque * Input.GetAxis ("VerticalVehicle");
    5.  
    6.         if (Input.GetKeyDown(KeyCode.LeftShift))
    7.         {
    8.             //1 is forward 0 is back
    9.  
    10.             changeGear(1);
    11.         }
    12.         else if (Input.GetKeyDown(KeyCode.LeftControl))
    13.         {
    14.             changeGear(0);
    15.         }
    16.  
    17.  
    18.  
    19.         foreach (AxleInfo axleInfo in axleInfos)
    20.         {
    21.             if (axleInfo.steering)
    22.             {
    23.                 axleInfo.leftWheel.steerAngle = steering;
    24.                 axleInfo.rightWheel.steerAngle = steering;
    25.             }
    26.  
    27.             if (axleInfo.motor)
    28.             {
    29.                 //axleInfo.leftWheel.motorTorque = motor;
    30.                 //axleInfo.rightWheel.motorTorque = motor;
    31.  
    32.                 if (currentGear == 2)
    33.                 {
    34.                     if (Input.GetAxis("verticalVehicle") > 0)
    35.                     {
    36.                         axleInfo.leftWheel.motorTorque = motor;
    37.                         axleInfo.rightWheel.motorTorque = motor;
    38.  
    39.                     }
    40.                     else
    41.                     {
    42.                         axleInfo.leftWheel.motorTorque = idleMotor;
    43.                         axleInfo.rightWheel.motorTorque = idleMotor;
    44.                     }
    45.  
    46.                 }
    47.                 else if (currentGear == 0)
    48.                 {
    49.                     if (Input.GetAxis("VerticalVehicle") > 0)
    50.                     {
    51.                         axleInfo.leftWheel.motorTorque -= backwardMotor;
    52.                         axleInfo.rightWheel.motorTorque -= backwardMotor;
    53.                     }
    54.                     else
    55.                     {
    56.                         axleInfo.leftWheel.motorTorque -= idleMotor;
    57.                         axleInfo.rightWheel.motorTorque -= idleMotor;
    58.                     }
    59.                 }
    60.                 else if (currentGear == 1)
    61.                 {
    62.                     //DO ABSOLUTELY NOTHING
    63.                 }
    64.  
    65.  
    66.             }
    67.  

    Anyone able to find what I'm doing wrong?
    Feel free to ask for more information or explanation!

    Thanks!
     
    Last edited: Dec 6, 2015
  2. mads232

    mads232

    Joined:
    Sep 6, 2013
    Posts:
    48
    Anyone?
     
  3. mads232

    mads232

    Joined:
    Sep 6, 2013
    Posts:
    48
    By doing some debugging myself, it does seem to be something affected by my gear script. Somehow, the input is not very responsive, and it takes several key presses, before it decides to actually switch gear.
     
  4. mads232

    mads232

    Joined:
    Sep 6, 2013
    Posts:
    48
    I was calling my functions in fixedUpdate, so after changing that to Update, it stopped doing that. (derp)