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:
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.
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.
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);
}
}