- Home /
The question is answered, right answer was accepted
How to make car accelerate/decelerate over time
So I'm attempting to make a realistic feeling car. I've got it running, turns feel alright, just how do I make it break appropriately, speed up appropriately, etc etc. I tried, not much luck, but it does stop for a second, go to 8 speed, then go to 30.
//inspector variables
var speed:float; //speed of the car, tweek as needed based on your scale
var turnSpeed:float; //turn speed
var timecar:float;
var accel: float;
function Update()
{
timecar += Time.deltaTime;
//grab the input axes
var steer=Input.GetAxis("Horizontal");
var gas=Input.GetAxis("Vertical");
//if they're hittin' the gas...
if (gas!=0)
{
InvokeRepeating("addspeed", 1.0, 1.0);
//take the throttle level (with keyboard, generally +1 if up, -1 if down)
// and multiply by speed and the timestep to get the distance moved this frame
var moveDist=gas*speed*Time.deltaTime;
//now the turn amount, similar drill, just turnSpeed instead of speed
// we multiply in gas as well, which properly reverses the steering when going
// backwards, and scales the turn amount with the speed
var turnAngle=steer * turnSpeed * Time.deltaTime * gas;
//now apply 'em, starting with the turn
transform.rotation.eulerAngles.y+=turnAngle;
//and now move forward by moveVect
transform.Translate(Vector3.forward*moveDist);
}
}
function addspeed (){
if (speed < 30){
speed++;
}
}
I've never tried making a racing game in Unity, so I'm completely new to this. I do however have a good amount of experience I Unity as a whole. Thanks for helping!
Just a side-note, the Car Demo provided by Unity won't work, and I didn't want to use it anyways, I prefer making games from scratch.
Answer by clunk47 · Dec 16, 2012 at 02:58 AM
Try making an acceleration var float. Like:
var accel : float = 1.0f;
then if(gas) speed+= Input.GetAxis("Vertical") * accel;
Of course this isn't formatted "code", it's Just an example of something you could try...
Sorry, do you mean like: var accel: float; if (gas = true) { speed+=Input.GetAxis("Vertical")*accel; }
Doesnt seem to work...
This isn't a copy and paste script, it is an example you could use to implement into your script. I would write out a script for you but I am about to get some sleep. If you don't figure it out by tomorrow I will work with you more on this :)
alright, Yours looks the best honestly, so I'll try to alter yours. If a variation of yours works, Ill give you a check!
Thanks,
Just in case anyone else reference's this post in the future. I took the advice from @clunk47 and it would look something like this. Of course, you can add a Time adjustment in or even use velocity but, this is a quick way to get the "acceleration/deceleration" going.
float speed; float acceleration = 1f;
if (Input.GetKey(KeyCode.W) && speed <= 30) { transform.Translate(Vector3.forward Time.deltaTime (speed += accel) verticalInput); } else if (Input.GetKey(KeyCode.W) && speed >= 30) { speed = 30; transform.Translate(Vector3.forward Time.deltaTime speed verticalInput); } else { speed = 0; }
Answer by aburningflame · Dec 16, 2012 at 03:46 AM
Not sure what youre trying to accomplish but if if (Input.GetKeyDown("s")) { speed = 1; while (speed < 16){ speed++; timecar = 0; }
is in an Update function..then if s is pressed speed will always be 16. (Because in 1 tick of update you are looping WHILE speed<16...so that loop will execute 15+ times in 1 update tick - forcing your speed to 16 right away.
You should just do: private int maxSpeed=16; if (Input.GetKeyDown(KeyCode.S)){ speed++; if (speed>maxSpeed) speed=maxSpeed; }else{ speed--; //decelerate speed maybe? }
This allows acceleration over time I suppose.
Also doesn't work...
Its not based on time,
What am I doing wrong?
sorry im not sure i understand. Update() is called every game tick. if S is down, you want to increase your speed. You may need to speed++ every second or so. Im not too sure what youre tryin gto do so its hard to help.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Mario Kart Style Drifting 0 Answers
How is wheelCollider RPM calculated exactly? 1 Answer
How to add drift to top down car mechanics? 0 Answers
Car exhaust Flame in unity 1 Answer