Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Spider_newgent · Nov 30, 2016 at 09:44 AM · pathfindingwaypoint systemvector3.movetowardspoint-and-clickdijkstra

Vector3.MoveTowards gives inconsistent speed when pathfinding between waypoints.

My player object moves to a target position via a series of waypoints. If he has direct line of sight to the target, he moves at the desired speed. If he can't see the target, he pathfinds to it via a series of viable waypoints. However, he moves between each waypoint at an erratic speed. Sometimes zipping there quickly, sometimes very slow. I've laid out my setup below, but if any further code etc is required, please let me know.

I'm making a point and click game and move my player using a Point of Visibility pathfinding system similar to this one: http://www.david-gouveia.com/portfolio/pathfinding-on-a-2d-polygonal-map/ (effectively the graph is made from the vertices of obstacles, rather than a square or hex grid).

This all works fine and I pass a series of waypoints to my player in the form of a list:

 public List<Vector3> wayPoints = new List<Vector3>();

If the players position does not equal the next waypoint on the list, he moves toward it using Vector3.MoveTowards like so:

 case State.Walking:
                 Debug.Log(playerState);
                 float step = speed * Time.deltaTime;
 
                 if (transform.position != destination) {
                     SetDirection();        //this tells the sprite which way to face, (N E S W)
                     transform.position = Vector3.MoveTowards(transform.position, destination, step);            
                 }
                 
                 if (transform.position == destination) {
                     wayPoints.RemoveAt(0);
 
                     if (wayPoints.Count > 0) {
                         destination = wayPoints[0];
                         dirCheckReqd = true;
                     }
 
                     if (wayPoints.Count <= 0) {
                         playerState = State.Idle;
                     }
                 }
                 break;

I'm calling SetState() in Update() so Vector3.MoveTowards shouldn't have any problems. Has any encountered this before, or have any suggestions about how to fix it?

Thanks

Dan

Comment
Add comment · Show 1
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 Spider_newgent · Nov 30, 2016 at 05:08 PM 0
Share

I've moved this to the "Default Space" in the hope that it'll attract more responses. Sorry if that's not the thing to do. Please let me know if that's a breach of any rule.

Cheers

Dan

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Nu-Lif · Aug 04, 2020 at 04:17 AM

Vector3.Movetowards moves in 3 dimensions, so if each your waypoints have a different position.z value, the step that your player takes to get to it is made in the X, Y, and Z directions. In a 2D game this may make it appear as though your object is moving more slowly toward its destination.

So to see if this is what is wrong with your game, make sure that all of the waypoints have the same position.z.

Comment
Add comment · Show 1 · 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 thepigeonfighter · Dec 16, 2021 at 02:05 PM 0
Share

This was exactly my problem, thanks <3

avatar image
0

Answer by Bunny83 · Dec 01, 2016 at 02:02 AM

I can't see anything wrong in the code you provided. Have you already tried common debugging strategies? For example try adding temporarily a debug log statement like this:

 Vector3 old = transform.position ;
 transform.position = Vector3.MoveTowards(transform.position, destination, step);
 Vector3 diff = transform.position - old;
 Vector3 dist = destination - transform.position;
 Debug.Log("set speed: " + speed + " actual speed: " + (diff.magnitude/Time.deltaTime)+" pos: " + transform.position + " destination : " + destination + " Distance: " + dist.magnitude);

This block should replace the MoveTowards line. This log statement should show:

  • if your speed variable is actually constant

  • what your actual speed is each frame (can be a bit "jerky" but should be around "speed")

  • where you player position is each frame

  • if destination actually is constant while you move towards it.

Try to run this and report back if you found anything suspicious.

Comment
Add comment · Show 1 · 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 Spider_newgent · Dec 03, 2016 at 07:24 PM 0
Share

Hi, thanks for the response.

I did find something suspicious. Imagine the player is walking from point A to point B, which is a distance of 6. If the player has a direct line of sight they move at the required speed, no problem.

Now imagine walking from A to C, which is just around a corner. This requires walking from A to the node at the corner, then from the corner to C, a total distance of say 6.2. In this instance, the "dist.magnitude" is out of whack and reports a distance of 90 or so. It's not logging the total path distance, the numbers don't add up for that, so why does dist.magnitude give a different result when there's more than one waypoint in the list?

In answer to your other questions:

  • Speed is constant.

  • The actual speed is very close to "speed"

  • The player position is as would be expected.

  • The destination is constant, (at least until a waypoint is reached and it is changed to the next in the list).

If there's any other code etc that would prove useful let me know and I'll post it.

Thanks again

Dan

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

82 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Implementation of Navmesh terrain dijkstra algorithm 0 Answers

Understanding Dijkstra Application 2 Answers

Pathfinding in 2d game 4 Answers

Train Track pathfinding - Navmesh? Waypoints? Spline? 1 Answer

Indexing tiles for pathfinding 1 Answer


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