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): using UnityEngine; using System.Collections; [RequireComponent(typeof(Rigidbody))] public class Motor : MonoBehaviour { public Transform centerOfMass; public float enginePower = 20f; public float turnPower = 20f; public float brake = 0.0f; /* * make sure the wheels are in the order of: * front left * front right * back left * back right */ public Wheel[] wheel; Rigidbody rbody; void Awake() { rbody = GetComponent<Rigidbody>(); } void Start () { rbody.centerOfMass = centerOfMass.localPosition; } void FixedUpdate() { float torque = Input.GetAxis("Vertical") * enginePower; float turnSpeed = Input.GetAxis("Horizontal") * turnPower; brake = Input.GetKey("space") ? GetComponent<Rigidbody>().mass * 0.1f : 0.0f; //all wheel drive //wheel[0].Move(torque); //wheel[1].Move(torque); //rear wheel drive wheel[2].Move(torque); wheel[3].Move(torque); //front wheel steering wheel[0].Turn(turnSpeed); wheel[1].Turn(turnSpeed); //rear wheel steering //wheel[2].Turn(turnSpeed); //wheel[3].Turn(turnSpeed); if (brake > 0.0) { wheel[0].brakeTorque = brake; wheel[1].brakeTorque = brake; wheel[2].brakeTorque = brake; wheel[3].brakeTorque = brake; wheel[2].torque = 200f; wheel[3].torque = 20f; } else wheel[0].brakeTorque = 0; wheel[1].brakeTorque = 0; wheel[2].brakeTorque = 0; wheel[3].brakeTorque = 0; wheel[2].torque = enginePower; wheel[3].torque = enginePower; } }
Right after you assign brake, what does a debug log tell you it equals? Also maybe try "Input.GetKey (Keycode.Space)" instead of a string. Another note, use code tags: http://forum.unity3d.com/threads/using-code-tags-properly.143875/
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.
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?
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.
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.
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.
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...
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
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): using UnityEngine; using System.Collections; using System; [RequireComponent(typeof(WheelCollider))] public class Wheel : MonoBehaviour { const string TIRE_NAME = "Tire"; [SerializeField]Transform tire; WheelCollider wc; internal float brakeTorque; public float torque { get; internal set; } void Awake() { wc = GetComponent<WheelCollider>(); tire = transform.FindChild(TIRE_NAME); } public void Move(float value) { wc.motorTorque = value; } public void Turn(float value) { wc.steerAngle = value; tire.localEulerAngles = new Vector3(0f, wc.steerAngle, 90f); } internal float Move() { throw new NotImplementedException(); } }
I dont see the brakeTorque being used anywhere... are you actually setting the brakeTorque on the wheelcollider, because It doesnt appear so.
From the wheel script I posted i have: internal float brakeTorque; Does this not set the brakeTorque on the wheelcollider?
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
Ok....thanks...I will give it shot and try to figure it out. As you probably gathered, I am learning as I go.
If you need some inspiration, there's a fairly comprehensive (but arcadish) script in my sig (basic wheelcollider controller)