Search Unity

My Brakes don't work

Discussion in 'Scripting' started by TImHighway, Jun 27, 2016.

  1. TImHighway

    TImHighway

    Joined:
    Jun 21, 2016
    Posts:
    7
    Hello,

    Could someone take at look at my C# script and tell me why the brakes on my car are not working?
    I have the script assigned to my car and everything works except the brakes.

    I get no errors in the console when the script runs.


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(Rigidbody))]
    6. public class Motor : MonoBehaviour {
    7.     public Transform centerOfMass;
    8.     public float enginePower = 20f;
    9.     public float turnPower = 20f;
    10.     public float brake = 0.0f;
    11.  
    12.     /*
    13.      * make sure the wheels are in the order of:
    14.      * front left
    15.      * front right
    16.      * back left
    17.      * back right
    18.      */
    19.  
    20.     public Wheel[] wheel;
    21.  
    22.     Rigidbody rbody;
    23.  
    24.     void Awake() {
    25.         rbody = GetComponent<Rigidbody>();
    26.     }
    27.  
    28.     void Start () {
    29.         rbody.centerOfMass = centerOfMass.localPosition;
    30.     }
    31.     void FixedUpdate()
    32.     {
    33.         float torque = Input.GetAxis("Vertical") * enginePower;
    34.         float turnSpeed = Input.GetAxis("Horizontal") * turnPower;
    35.         brake = Input.GetKey("space") ? GetComponent<Rigidbody>().mass * 0.1f : 0.0f;
    36.  
    37.         //all wheel drive
    38.         //wheel[0].Move(torque);
    39.         //wheel[1].Move(torque);
    40.  
    41.         //rear wheel drive
    42.         wheel[2].Move(torque);
    43.         wheel[3].Move(torque);
    44.  
    45.         //front wheel steering
    46.         wheel[0].Turn(turnSpeed);
    47.         wheel[1].Turn(turnSpeed);
    48.  
    49.         //rear wheel steering
    50.         //wheel[2].Turn(turnSpeed);
    51.         //wheel[3].Turn(turnSpeed);
    52.  
    53.         if (brake > 0.0)
    54.         {
    55.  
    56.             wheel[0].brakeTorque = brake;
    57.             wheel[1].brakeTorque = brake;
    58.             wheel[2].brakeTorque = brake;
    59.             wheel[3].brakeTorque = brake;
    60.             wheel[2].torque = 200f;
    61.             wheel[3].torque = 20f;
    62.         }
    63.         else
    64.  
    65.             wheel[0].brakeTorque = 0;
    66.             wheel[1].brakeTorque = 0;
    67.             wheel[2].brakeTorque = 0;
    68.             wheel[3].brakeTorque = 0;
    69.             wheel[2].torque = enginePower;
    70.             wheel[3].torque = enginePower;
    71.  
    72.     }        
    73.  
    74. }
    75.  
     
    Last edited: Jun 27, 2016
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Last edited: Jun 27, 2016
  3. TImHighway

    TImHighway

    Joined:
    Jun 21, 2016
    Posts:
    7
    Hi Riptile thanks for the suggestion. I tried Keycode.Space with the same result. Still doesn't work.
    In the inspector under the script, I have a field for Brake. The value of the field is initially "0" when I run the game and it changes to 5 when press the spacebar. But, the car does not slow down.
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    So what does assigning the wheel[0].brakeTorque to 5 do? Is this a built in wheel collider or something or do you use an asset for vehicle physics?
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Your else seems to be missing brackets. I'm assuming all that after the else is suppose to be part of the else? Basically, what is happening is your if might be triggering, but your else only triggers the first line and not all the other stuff with it.
    So for example, wheel[1].brakeTorque = brake, but then right after, wheel[1].brakeTorque = 0; So except for wheel[0], everything gets reset.
     
  6. TImHighway

    TImHighway

    Joined:
    Jun 21, 2016
    Posts:
    7
    Assigning wheel[0].brakeTorque to 5 does nothing. That's my problem. It does nothing for all of the car's tires.

    wheel[0] is the FR tire on my car which does have a wheelCollider and it is working properly.

    I am not sure what you mean by using an asset for vehicle physics. I am using the physics engine built into Unity.
     
  7. TImHighway

    TImHighway

    Joined:
    Jun 21, 2016
    Posts:
    7
    Hi Brathann,
    Thanks, you are right. I was missing the { } for my else statement. Putting them in does not make a difference though. I still have no brakes.
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    show us your wheel class.

    the problem isnt in the code you posted, unless brake torque is insufficient, however I notice you assigned torque of 200/20 when you set your brakes. Torque should be zero...
     
  9. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    What sort of mass are you using?

    Looking at my script in sig, I use this to determine brake power...

    wheelCollider.brakeTorque=Mathf.Clamp(Mathf.Abs(wheelCollider.rpm)* 8f, 100, 10000);

    which works well for a 1000Kg vehicle. Of course things like mass of wheelcollider also affect the effectiveness of brake/torque
     
  10. TImHighway

    TImHighway

    Joined:
    Jun 21, 2016
    Posts:
    7
    Hi James,

    Thanks, The Torque is set to 0.0f as you mentioned. The 200/20 values were there because I was testing different settings to try to make it work and failed to set it back to 0 before i posted the script.

    My wheel script is below:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5.  
    6. [RequireComponent(typeof(WheelCollider))]
    7. public class Wheel : MonoBehaviour {
    8.     const string TIRE_NAME = "Tire";
    9.     [SerializeField]Transform tire;
    10.     WheelCollider wc;
    11.     internal float brakeTorque;
    12.  
    13.     public float torque { get; internal set; }
    14.  
    15.     void Awake()  {
    16.         wc = GetComponent<WheelCollider>();
    17.         tire = transform.FindChild(TIRE_NAME);
    18.  
    19.     }
    20.  
    21.     public void Move(float value)    {
    22.         wc.motorTorque = value;
    23.     }
    24.  
    25.     public void Turn(float value)  {
    26.         wc.steerAngle = value;
    27.         tire.localEulerAngles = new Vector3(0f, wc.steerAngle, 90f);
    28.      
    29.     }
    30.  
    31.     internal float Move()
    32.     {
    33.         throw new NotImplementedException();
    34.     }
    35. }
    36.  
     
    Last edited: Jun 27, 2016
  11. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I dont see the brakeTorque being used anywhere...

    are you actually setting the brakeTorque on the wheelcollider, because It doesnt appear so.
     
  12. TImHighway

    TImHighway

    Joined:
    Jun 21, 2016
    Posts:
    7
    From the wheel script I posted i have: internal float brakeTorque;

    Does this not set the brakeTorque on the wheelcollider?
     
  13. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    nope.

    You need to set it up like Turn, where you are setting the value on the wheelcollider reference, not some internal variable in your class
     
  14. TImHighway

    TImHighway

    Joined:
    Jun 21, 2016
    Posts:
    7
    Ok....thanks...I will give it shot and try to figure it out.

    As you probably gathered, I am learning as I go. :)
     
  15. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    If you need some inspiration, there's a fairly comprehensive (but arcadish) script in my sig (basic wheelcollider controller)