Hi there,
I have a basic AI script for my CPU controlled cars that requires LOADS of waypoints as I am using MoveTowards… which is very linear. I tried Slerp but that just did all kinds of crazy stuff. Is there an easy way to smooth out the transition between waypoints?
Here’s my code:
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.CompareTag ("Waypoints")) // Checks if colliding with waypoint via tag
{
print(Waypoints[iIndex]);
// When a waypoint is hit, increase array index so car moves on
if (iIndex < (Waypoints.Length - 1))
{
++iIndex;
}
else
{
iIndex = 0; // Move car back to Waypoint 01
}
}
}
// TODO set up brake zone collision areas
function Update ()
{
if (!LevelSetup.g_bGameIsPaused && LevelSetup.g_bGameInPlay == true)
{
// ACCELERATION
if (!bCarIsBraking)
{
if (fSpeed < fMaxSpeed)
{
fSpeed += fAcceleration;
}
else
{
fSpeed -= fBrakeForce;
}
transform.position = Vector3.MoveTowards(transform.position,Waypoints[iIndex].transform.position,(fSpeed * Time.deltaTime));
}
}
}
Cheers!