Smooth Movement Along Waypoints & Update() vs FixedUpdate()

There are several questions like this, but I think I am having a specific problem with my code or approach I can’t spot…

I am moving bots (“Agents”) on a Journey, which is comprised of a List of waypoints. The following code works – there is nice glassy smoothness to the movement between individual waypoints. Problem is a noticeable stop as they reach one waypoint before starting movement toward the next, which spoils the overall effect, as I don’t want the waypoints to be noticeable.

If I change the action to run in FixedUpdate() not Update(), that problem seems to go away – but instead, the overall motion between waypoints is no longer nice and smooth and seems jittery. The Agents do not have rigidbodies or do any physicsy stuff, that and performance is why I shied away from FixedUpdate() initially.

So how can I get smooth movement throughout the journey please?

namespace Agent
{
	public class AgentController : MonoBehaviour
	{
		public event System.Action JourneyComplete;

		const float MOVEMENT_SPEED = 2.5f;
		const float LOOK_WHERE_GOING_ROTATION_SPEED = 10f;

		Vector3 startPoint;
		Vector3 endPoint;
		float startTime;
		float journeyDistance;
		bool isUnderway;
		int waypointsReached;
		Vector3 currentPos;

		Vector3 directionOfMovement = Vector3.zero;
		List<Vector3> waypoints = new List<Vector3> ();

		void Update ()
		{
			if (!isUnderway)
				return;

			Vector3 currentPos = transform.position;

			// Are we there yet?
			if (Vector3.Equals (currentPos, endPoint))
			{ 
				waypointsReached++;

				if (waypointsReached == waypoints.Count)
				{
					isUnderway = false;
					System.Action handler = JourneyComplete;
					if (handler != null)
						handler ();
					
				} else {
					MoveToWaypointIndex (waypointsReached);
				}
			}

			if (journeyDistance == 0)
				return;

			float fracJourney = ((Time.time - startTime) * MOVEMENT_SPEED) / journeyDistance;
			transform.position = Vector3.Lerp (startPoint, endPoint, fracJourney);

			//Face the front, agent!
			if (directionOfMovement != Vector3.zero) {
				transform.rotation = Quaternion.Slerp (
					transform.rotation,
					Quaternion.LookRotation (directionOfMovement),
					Time.deltaTime * LOOK_WHERE_GOING_ROTATION_SPEED
				);
			}
		}


		public void MoveToWaypointIndex (int waypointIndex)
		{
			startPoint = transform.position;
			endPoint = waypoints [waypointIndex];
			isUnderway = true;
			startTime = Time.time;
			journeyDistance = Vector3.Distance (startPoint, endPoint);
			directionOfMovement = endPoint - startPoint;
		}
...

@gorsefan … You need to establish a ‘close enough’ distance to the current endPoint to trigger a move to the next waypoint. Your code…

     // Are we there yet?
     if (Vector3.Equals (currentPos, endPoint))

… is looking for equality and as you are lerping to the end point it may take several iterations to actually reach equality and move on.

I usually check the distance to the waypoint like this…

     // Are we there yet?
     if (Vector3.Distance(currentPos, endPoint) <= minDistance)

… so that when they get close enough … the endPoint is updated to the next waypoint.

minDistance could be a hard coded value, a class property, or calculated based on varing aspects of the moving object (e.g. size, speed, turn rate, etc.).

DiGiaCom Tech’s answer certainly put me on the right track. After doing some more research, and testing at a variety of framerates I’ve found I’m getting smoother results moving forward at a constant speed and Quaternion.Slerp-ing rotation. So at the moment my code looks like this, and works for me. I’m looking for tron bike-style movement along a grid so the variables meet this use-case, but with a lower LOOK_WHERE_GOING_ROTATION_SPEED and higher WAYPOINT_REACHED_DISTANCE I think it could be adapted for cars, aeroplanes etc.

		const float MOVEMENT_SPEED = 2.5f;
		const float LOOK_WHERE_GOING_ROTATION_SPEED = 5f;
		const float WAYPOINT_REACHED_DISTANCE = 0.75f;

	/* POTENTIAL BUG WARNING
	 * If the LOOK_WHERE_GOING_ROTATION_SPEED && WAYPOINT_REACHED_DISTANCE are 
	 * too low the Agent does not rotate in time to hit the waypoint and 
	 * will run off over the horizon. Can belt & braces this by checking 
	 * if current distance to endPoint is increasing not decreasing, but 
	 * that is more vector calcs per frame, so instead i keep an eye on it.
	 */

		/// <summary>
		/// Fires when waypoints are exhausted
		/// </summary>
		public event System.Action JourneyComplete;

...
		void Update ()
		{

			if (!isUnderway)
				return;

			// Are we nearly there yet?
			if (Vector3.Distance (transform.position, endPoint) < WAYPOINT_REACHED_DISTANCE)
			{ 
				waypointsReached++;

				if (waypointsReached == waypoints.Count)
				{
					isUnderway = false;
					System.Action handler = JourneyComplete;
					if (handler != null)
						handler ();
					
				} else {
					MoveToWaypointIndex (waypointsReached);
				}
			}

			if (journeyDistance == 0)
				return;

			// Face the front, agent!
			if (directionOfMovement != Vector3.zero) {
				transform.rotation = Quaternion.Slerp (
					transform.rotation,
					Quaternion.LookRotation (directionOfMovement),
					Time.deltaTime * LOOK_WHERE_GOING_ROTATION_SPEED
				);
			}

			// Constantly moving forward whilst turning helps hugely with smooth movement
			transform.Translate(Vector3.forward * MOVEMENT_SPEED * Time.deltaTime);
		}