- Home /
Unity Car tutorial Brake
Hi,
I am using the car and some scripts from the Unity3d car tutorial, and when I accelerate then release the button, the car keeps on going, and even after I brake it, it rolls. How can I set it to decelerate faster,and to stay stopped?
Im not familiar with this tutorial...so im not sure how acc/dec are handled(nor have i ever made a car game). Im sure there is a var in the script that handles the cars movement named deceleration(or something of the sorts) that you can tweak to your liking. But i cant really say what it is without digging through the scipts on the tutorial.
Answer by hijinxbassist · Mar 17, 2012 at 12:36 AM
Ok, so its pretty complex as expected. Found the line where the braking takes place. Line 527 of the car script(java).
rigidbody.AddForce(transform.forward * Time.deltaTime * (throttleForce + brakeForce));
Here are the 2 vars that handle the acc/dec of the car.
NOTE: If you are using this script w/o changes, acc/dec will be at the same rate(the posted line handles both acc/dec). So if you are trying to stop quicker than you accelerate, it might be a good idea to make an add on script that applies additional stopping force.
Theres a few ways to add extra brake force. You could add a boolean to the car script called braking and make this true when the throttle force is negative.
public var braking:boolean;
function Update()
{
if(throttleForce<0)braking=true;
else braking=false;
}
then from the new script
public var braking:boolean;
public var extraBrakeForce:int;
function FixedUpdate()
{
braking=transform.GetComponent(Car).braking;
if(braking)rigidbody.AddForce(transform.forward*Time.deltaTime*(-extraBrakeForce));
}
Not tested, but should work. Dont forget to set your extra brake force from inspector. Let me know if it works out ok.
That works when I am braking, but not when the car is just rolling. Also, I had to change throttleforce to throttle
Right right, since it is extra brake force. You could add some more force when braking isnt true. like...
public var braking:boolean;
public var extraBrakeForce:int;
public var extraDrag:int;
function FixedUpdate()
{
braking=transform.GetComponent(Car).braking;
if(braking)rigidbody.AddForce(transform.forward*Time.deltaTime*(-extraBrakeForce));
else if(!Input.GetAxis("Vertical")&&rigidbody.velocity.magnitude>0)
{
rigidbody.AddForce(transform.forward*Time.deltaTime*(-extraDrag));
}
}
or use the same var ins$$anonymous$$d and divide it by an amount...unless you want the full extra brake force applied as drag. Basically what the new line says is...
If braking is false (else), if you are not pushing the vertical button (!Input.GetAxis("Vertical")), and if the car is actually moving still (rigidbody.velocity.magnitude>0).
I hope this explanation will help you understand how i accomplished this.
Assets/Scripts/JavaScripts/Car.js(539,10): BCE0044: expecting (, found 'FixedUpdate'.
Im trying to get this to work in Android ,any idea why I get this error please ? :)
Your answer
Follow this Question
Related Questions
Car Tutorial Issue .......... 5 Answers
When I spawn the car, I can not hear the sound. Why 0 Answers
How do I download the new unity 5 car tutor? 1 Answer
Unity Car Tutorial Error 0 Answers
Making a race car positioning system 5 Answers