MoveTowards... a smoother way?

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!

You probably want to calculate a hermite spline.

Found this link on google, have not tried it myself so can’t take any responsibility
https://www.auto.tuwien.ac.at/wordpress/?p=495

Or you could make your own function using the math on Hermite Curve Interpolation

A cheap way is to always move forwards, but smooth-in your rotation. In other words, when you get close to a waypoint, the car starts to turn towards the next one. This is from a working project (which didn’t look too bad):

Vector3 dest2 = dest; dest2.y = transform.position.y; // only rotate around y
Quaternion wantFace = Quaternion.LookRotation(dest2 - transform.position);
transform.rotation = Quaternion.RotateTowards(transform.rotation,
              wantFace, 90 * Time.fixedDeltaTime);
// 90 is degrees/sec

Then just set velocity or position based only on transform.forward.