Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gorsefan · May 24, 2016 at 06:24 PM · smoothfixedupdateupdate functionwaypoints

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;
         }
 ...
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vintar · May 24, 2016 at 06:42 PM 0
Share

Probably because you are lerping. Try use fixed update and just move the transform a small amount every frame.

avatar image gorsefan vintar · May 24, 2016 at 09:28 PM 0
Share

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!

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.).

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gorsefan · May 24, 2016 at 09:23 PM 0
Share

Aha yes, thank you very much!

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

avatar image DiGiaCom-Tech gorsefan · May 24, 2016 at 09:55 PM 0
Share

@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)?

avatar image gorsefan DiGiaCom-Tech · May 28, 2016 at 11:24 AM 0
Share

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 :)

avatar image
0

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);
         }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I make it so that a different set of code is called in Update() depending on a variable determined in the Start() function? 2 Answers

waypoint script, how to remove lerp easing 1 Answer

Raycast and Physics(Ragdolls): Update() or FixedUpdate()? 1 Answer

Unity Physics frame rate. void FixedUpdate 3 Answers

how to change a value over time in a coroutine ? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges