I have a car that can accelerate and decelerate but the problem is when I’m not pressing ‘W’ the car is very slowly going backwards. I have the acceleration clamped so it shouldn’t have a negative value but it is. Please help.
using UnityEngine;
using System.Collections;
public class CarModelControl : MonoBehaviour {
float Acceleration;
float MinAcceleration = 0.0f;
public float MaxAcceleration = 30.0f;
public float AccelerationRate = 5.0f;
public float DecelerationRate = 10.0f;
public float rotateSpeed;
public float ReverseSpeed;
void Start () {
}
void Update () {
Acceleration = Mathf.Clamp(Acceleration, MinAcceleration, MaxAcceleration);
if (Input.GetKey(KeyCode.W))
{
Acceleration = Acceleration + (AccelerationRate * Time.deltaTime);
transform.Translate(Vector3.forward * Acceleration * Time.deltaTime);
Debug.Log(Acceleration);
}
else
{
Acceleration = Acceleration - (DecelerationRate * Time.deltaTime);
transform.Translate(Vector3.forward * Acceleration * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.down * rotateSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.back * ReverseSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
}
}
}