- Home /
Car Tutorial Issue ..........
Hi there ,Im begging on my knees now ,all I want is to make the Unity Car Tutorial Function properly ,as in when no keys are touched the car should stop but it doesnt :(
Matthew A said that this piece of code should fix the problem .
There is also a bug in Car.js which I think exacerbates this problem (the car accelerates when the throttle isn't being pressed). I believe the logic there should be something like this instead:
if (throttle == 0) { // Mathf.Sign returns 1 when a value is 0, (also happens in HaveTheSameSign) // this causes inappropriate acceleration unless we check whether throttle is 0 first... :-/ } else if (HaveTheSameSign(relativeVelocity.z, throttle)) { if (!handbrake) { throttleForce = Mathf.Sign(throttle) currentEnginePower rigidbody.mass; } } else { brakeForce = Mathf.Sign(throttle) engineForceValues[0] rigidbody.mass; }
Has anybody any Idea where to put this piece of code ,I am tearing my hair out just trying to get this demo to work ,please please please someone help me out ,I have been trying to sort this out for months but noone seems to know the answer :(
Answer by whydoidoit · May 26, 2012 at 05:19 PM
Well as you are desperate I've given it a go.
Looks to me like there isn't much friction going on, not enough to overcome something in the simulation that keeps it rolling. I guess the answer is either to do something with the forwardFriction of the WheelCollider (but Googling implies that there might be problems here) or to apply some level of braking when there is no throttle.
Here's a stab at that:
function ApplyThrottle(canDrive : boolean, relativeVelocity : Vector3)
{
if(canDrive)
{
var throttleForce : float = 0;
var brakeForce : float = 0;
if(rigidbody.velocity.magnitude < 0.01 && throttle == 0) {
rigidbody.velocity = Vector3.zero;
} else {
if(relativeVelocity.z > 0.00001 && throttle == 0)
{
brakeForce = -21000 * relativeVelocity.z - 2000;
}
if(relativeVelocity.z < 0 && throttle ==0)
{
brakeForce = 20000 * relativeVelocity.z;
}
if(throttle != 0)
{
if (HaveTheSameSign(relativeVelocity.z, throttle))
{
if (!handbrake)
throttleForce = Mathf.Sign(throttle) * currentEnginePower * rigidbody.mass;
}
else
brakeForce = Mathf.Sign(throttle) * engineForceValues[0] * rigidbody.mass;
}
rigidbody.AddForce(transform.forward * Time.deltaTime * (throttleForce + brakeForce));
}
}
}
this is AWESO$$anonymous$$E! the autodriving was "driving" me crAzYY!!!! :)
one correction... when i applied this great script i had to also add a "-" in front of 20000 for me to get the car not to also auto drive backwards as well... hope this helps whoever else is battling this issue!
if(relativeVelocity.z < 0 && throttle ==0) { brakeForce = -20000 * relativeVelocity.z; }
Answer by mak · May 26, 2012 at 07:44 PM
Hey there Mike thanks for the reply :) can you tell me where I drop this into car.js please mate,I have tried different places but keep getting this error ?
Assets/Standard Assets/Scripts/Car.js(595,10): BCE0089: Type 'Car' already has a definition for 'ApplyThrottle(boolean, UnityEngine.Vector3)'.
ps im writing for Android if that makes any difference ?
Just find the line function ApplyThrottle and totally replace that routine
It's somewhere near line 510
Answer by mak · May 26, 2012 at 08:27 PM
Im redownloading the untouched car tut and ill get right back to you :) ,fingers crossed :)
silly question im on latest unity 3.5etc would that make any difference ?
Just a note: you shouldn't post answers unless you are answering things :) Just use the comments - it messes up your accepted score too!
Well I'm running 3.5.2 and that code made the car stop for me :) You can adjust those 20000 numbers to have less of an effect/more of an effect etc. I'm not sure this is the ideal way to have it work - the car model is doing stuff like Engine braking etc, but I couldn't make it stop that way...
lol ,have been all over the place with answers instaed of comments lol :P ,but once again thank you :) this was literally driving me nuts lol :)
Android ^^ ,it was trying to make the game run on Android/I-phone all along ,I feel like such a fool now lol ,ah well back to the drawing board I guess :p . Basically when I switch platform for Android the car rolls ... Im beginning to think its all just not possible :(
Answer by mak · May 26, 2012 at 08:39 PM
Woot ,dont take this the wrong way but I LOVE YOU !!!!!!!!!!!!!!!!!! Absolutely perfect THANK YOU !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Perferably flag me as answering it not yourself ;) !!!!!
Answer by kockaart · Apr 06, 2014 at 03:21 PM
check this out, sliding problem is solved as well:
function ApplyThrottle(canDrive : boolean, relativeVelocity : Vector3) { if(canDrive) { var throttleForce : float = 0; var brakeForce : float = 0;
if(rigidbody.velocity.magnitude < 0.3 && throttle == 0) {
rigidbody.velocity = Vector3.zero;
rigidbody.constraints = RigidbodyConstraints.FreezeAll;
} else {
rigidbody.constraints = RigidbodyConstraints.None;
if(relativeVelocity.z > 0.00001 && throttle == 0)
{
brakeForce = -21000 * relativeVelocity.z - 2000;
}
if(relativeVelocity.z < 0 && throttle ==0)
{
brakeForce = -20000 * relativeVelocity.z;
}
if(throttle != 0)
{
rigidbody.constraints = RigidbodyConstraints.None;
if (HaveTheSameSign(relativeVelocity.z, throttle))
{
if (!handbrake)
throttleForce = Mathf.Sign(throttle) * currentEnginePower * rigidbody.mass;
}
else
brakeForce = Mathf.Sign(throttle) * engineForceValues[0] * rigidbody.mass;
}
rigidbody.AddForce(transform.forward * Time.deltaTime * (throttleForce + brakeForce));
}
}
}
Your answer
Follow this Question
Related Questions
Stuck in Car tutorial - Wheels 0 Answers
Car movement script ? 1 Answer
need tutorials for rigidbody.Addforce 1 Answer