- Home /
Car not accelerating
I am new to unity... and javascript so I though maybe making a really simple car would be a great challenge! but for some reason... when I let go of forward my car just stops right away and then goes backwards.-- heres the script
//Moving
var rotateSpeed : int;
private var curSpeed : float = 1;
var maxSpeed : float = 100;
var maxBackSpeed : float = 30;
var accel : float = .4;
var decSpeed : float = .4;
function Update()
{
//Moving & Rotating the car
var yRotateSpeed : float = Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime;
var ifForward : float = Input.GetAxis("Vertical");
//Determining if the car is going forward AND Accelerating/Deaccelerating
//Going forwards
if (ifForward > 0)
{
curSpeed += accel;
if (curSpeed >= maxSpeed)
{
curSpeed = maxSpeed;
}
//Going backwards
//Need going backwards script
//********************************************************
}
// Decease Speed/Stopping
var idle : boolean = false;
if (ifForward == 0)
{
idle = true;
}else{
idle = false;
}
while (idle && curSpeed > 0)
{
curSpeed -= decSpeed;
}
//Current Speed Translate
transform.Translate(0, 0, curSpeed * Time.deltaTime);
transform.Rotate(0, yRotateSpeed, 0);
}
Answer by gribbly · Aug 27, 2012 at 08:25 AM
Try clamping curSpeed to 0.0f when decelerating.
I would set idle = false before you read the Vertical stick axis, then only set it to true if you enter the
if(ifForward > 0)
block.
You could also try wrapping your transform.Translate call like this:
if(!idle) {
transform.Translate(0, 0, curSpeed * Time.deltaTime);
}
Lastly, you should probably be using Time.deltaTime in when incrementing/decrementing curSpeed:
curSpeed += accel * Time.deltaTime; //adjust accel as needed
Your answer
Follow this Question
Related Questions
GUI will not disappear 1 Answer
Wheel rotation help cant work out the problem? 0 Answers
Accelerator and how to limit player movement (Android) 0 Answers
Car's Acceleration is stuck 1 Answer
Open Link in new window or tab 1 Answer