Hello. This is my first post so please be gentle.
I am working with Unity’s Car Tutorialand trying to make the car accelerate by itself. I disabled the throttle and break functions in Car.js so the player can only steer left or right. My problem is creating a function that will make the car accelerate until it reaches the maxSpeed. Currently, the car will slowly move forward and at the same speed. I am also unsure if I need to call the function in Update() or FixedUpdate(). Any help will be greatly appreciated.
var curSpeed : float = 0.0; // holds current speed value
var acc : float = 10.0; // rate car accelerates at
var maxSpeed : float = 160.0; // max speed the car can go
function FixedUpdate()
{
// The rigidbody velocity is always given in world space, but in order to work in local space of the car model we need to transform it first.
var relativeVelocity : Vector3 = transform.InverseTransformDirection(rigidbody.velocity);
CalculateState();
UpdateFriction(relativeVelocity);
UpdateDrag(relativeVelocity);
ApplyAcceleration(relativeVelocity);
ApplySteering(canSteer, relativeVelocity);
}
function ApplyAcceleration(relativeVelocity)
{
curSpeed += acc;
curSpeed = Mathf.Clamp(curSpeed,-maxSpeed,maxSpeed); // Clamps curSpeed
rigidbody.AddForce(transform.forward * curSpeed * Time.deltaTime);
}
Use ForceMode.Acceleration
– Zero_Zagarth