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
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));
}
}
}
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 ?
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 ?
Woot ,dont take this the wrong way but I LOVE YOU !!! Absolutely perfect THANK YOU !!!
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));
}
}
}