- Home /
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;
}
...
Probably because you are lerping. Try use fixed update and just move the transform a small amount every frame.
Getting this working smoothly in FixedUpdate() didn't seem to work correctly, and (after some more reading) FixedUpdate() is by design not in sync with the screen refresh rate, so I don't see how it ever could, reliably... but thanks for the suggestion!
Answer by DiGiaCom-Tech · May 24, 2016 at 07:35 PM
@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.).
Aha yes, thank you very much!
// Are we nearly there yet?
if (Vector3.Distance (transform.position, endPoint) < WAYPOINT_REACHED_DISTANCE)
{
transform.position = endPoint;
waypointsReached++;
@gorsefan ... I wouldn't set the object's transform.position to the destination's endpoint as this could cause a perceptible jump (depending on the distance).
In this example I provided, the object would simply start turning and keep moving towards wherever the new/next end point is.
Think of an airline pilot flying a cross country route ... they actually start turning BEFORE they get to the point. With practice (and in your case adjustments to WAYPOINT_REACHED_DISTANCE) they know exactly when to start the turn before reaching the end point so they roll out (stop turning) almost exactly on the new course line (the imaginary line between the point they are approaching and the next point down the road).
There are tons of refinements that can be added to this process (i.e. lerping the turn, calculating the WAYPOINT_REACHED_DISTANCE based on the angle between the two legs, etc.). Can you say fuzzy logic (e.g. difference feedback calculations)?
Yep this does cause a perceptible jump, I have tweaked the WAYPOINT_REACHED_DISTANCE so this is rarely noticeable, but it's not great and maybe might become a problem if the framerate is suffering? Anyways you make a good point, I should simply swap the waypoint for the next if there are any... thank you :)
Answer by gorsefan · Jun 07, 2016 at 11:31 AM
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);
}