- Home /
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
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
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.
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.
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
Follow this Question
Related Questions
A* waypoint movement for player 2 Answers
dijkstra's algorithm optimization 1 Answer
Specific 2D movement using Dijkstras Algorithm... 1 Answer
Implementation of Navmesh terrain dijkstra algorithm 0 Answers
Understanding Dijkstra Application 2 Answers