- Home /
How do I make a car accelerate in both directions?
EDIT : I now have the code for accelerating/decelerating In both directions. What I now need to know is how to make "rotateSpeed" 0 if "curSpeed" is 0. I have tried if(curSpeed = 0) { rotateSpeed = 0.0; }
But I get errors. any thoughts?
I have code to accelerate forwards, and decelerate, but this code for accelerating backwards doesn't work. Can anyone help me? Here is the code that doesn't work:
if(Input.GetKey("s")) { curSpeed -= accelerationAmount Time.deltaTime; transform.position += -transform.forward curSpeed * Time.smoothDeltaTime; }
For your edit; for an if statement you need to use == ins$$anonymous$$d of =
ie.
if (curSpeed == 0){ rotateSpeed = 0.0; }
Answer by adrenak · Sep 07, 2012 at 07:52 AM
You can always drive a car by changing the location in a mathematical manner as you have done above, however, in my experience, it is kinda analogous to looking for unnecessary trouble, you can get better effects by using the wheel colliders of Unity.
Plus, this tutorial : http://www.gotow.net/andrew/blog/?page_id=78
makes is pretty easy to assemble your own car...and drive away!
This is your first question in here, but don't worry if the very first answer that you get is about some strange WheelCollider thingie... it just takes an hour to follow that tutorial and later on the very same scripts can be perfectly changed according to your will to get any additional results that you want.
I hope this helps!
Vatsal
I would also recommend staying away from the car control script that the official Unity Car Tutorial contains
Thanks alot for your help! But if I wanted to do it in a mathematical manner, how would I go about doing that?
Answer by Strategos · Sep 07, 2012 at 03:53 PM
I would have your controls changing curSpeed as you do currently. so it is either a positive or negative number.
then afterwards have this line
transform.position += ((transform.forward curSpeed) Time.smoothDeltaTime);
That looks like it should work to me.
Ok, I tried your code line, but rather than accelerating backwards, curSpeed goes up and when I release the "s" key, the car flies backwards at that speed... Do you want my entire script?
You need to update the position every frame regardless of whether a key is being pressed.
You can slow it down when keys aren't being pressed , either slowly or quickly depending how much inertia you want.
Answer by SirBedlam · Sep 07, 2012 at 03:44 PM
EDIT
Better yet, try this. It's just for going backward, but it shouldn't be too hard to add forward commands too. :) (You still need a character controller on your car.)
var curSpeed : float = 2;
var accelerationAmount : float = 2;
var maxAcceleration : float = 2;
var slowDown : float = 1;
var maxSpeed : float = 20;
function Start () {
}
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
var backward : Vector3 = transform.TransformDirection(-Vector3.forward);
if (accelerationAmount > maxAcceleration)
{
accelerationAmount = maxAcceleration;
}
if(Input.GetKey("s") && curSpeed < maxSpeed)
{
curSpeed += accelerationAmount * Time.deltaTime;
controller.SimpleMove(backward * curSpeed);
}
if(!Input.GetKeyUp("s") && curSpeed > 0)
{
curSpeed -= slowDown * Time.deltaTime;
controller.SimpleMove(backward * curSpeed);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
audio fades 0 Answers
Unique Car Demo Controls? 1 Answer
use of GuiTextures? 0 Answers
Help with car 2-player Input 0 Answers