- Home /
Player doesn't reach waypoint location
My player currently traverses the nodes in the Stack of Vector3 called currentPath. The problem is that it seems to do it in one frame. However when I change the moveTowards function from speed to speed*Time.deltaTime, it goes at the correct speed but it only goes halfway towards a node before starting to head for the next, but never fully reaches them.
Update()
{
if (currentPath != null && currentPath.Count > 0)
{
if (transform.position!=currentPath.Peek())
{
transform.position = Vector3.MoveTowards(transform.position, currentPath.Peek(), speed);
}
else
{
currentPath.Pop();
if (currentPath.Count == 0)
{
moving = false;
}
}
}
}
I've tried adding a timer but it didn't seem to work Any help, I'd be really grateful. Thank you!
The last parameter of $$anonymous$$oveTowards is not meant to be a speed, it's "maxDistanceDelta". If the value of "speed" is higher than the distance the movement will happen in one call, that's what happens probably without the *TIme.deltaTime part.
Now, the behaviour when you add the *Time.deltaTime sounds weird. Have you tried to Debug.Log the transform.position and currentPath.Peek() or just call Debug.Break on the "else" block? Is it possible that other part of your code is changing the position in the same frame?
Answer by highpockets · Apr 23, 2019 at 10:03 AM
MoveTowards is just as easy as lerp, it’s just a different way of accomplishing the same thing. Lerp will interpolate between 2 Vector3’s linearly and MoveTowards will just move towards a Vector3 position by the amount you pass as the last parameter. I think since you are constantly changing your end positions based on click positions, MoveTowards is a better bet, because if you change your target halfway through a Lerp, it is no longer linear and the speed will change unless you do extra calculations to compensate for the non linear movement.
If it is doing it all in one frame, your speed/maxDistanceDelta value is much to high. You should lower the speed value first of all and to make sure that you at least spend one frame at each waypoint you should check if speed is mor than the distance from the player to the waypoint and if it is, that frame, you should just move the length of distance rather than speed.
Cheers
Answer by Magso · Apr 22, 2019 at 08:36 PM
I know this isn't similar to what you have but it would be simpler to use lerps like this
public Transform[] points;
public float distanceTravelled;
public float speed;
void Update()
{
distanceTravelled += speed;
if(distanceTravelled < 1)
{
transform.position = Vector3.Lerp(points[0].position, points[1].position, distanceTravelled);
}
else if(distanceTravelled < 2)
{
transform.position = Vector3.Lerp(points[1].position, points[2].position, distanceTravelled-1);
}
//and so on.
}
or use an int variable to manage the points if the path is only followed one way.
public int currentPoint;
void Update()
{
distanceTravelled += speed;
if(distanceTravelled < currentPoint+1)
{
transform.position = Vector3.Lerp(points[currentPoint].position, points[currentPoint+1].position, distanceTravelled-currentPoint);
}
else{
currentPoint++;
}
}
Hi, Sorry, I forgot to specify, the currentPath stack varies. It isn't fixed. This is part of my Waypoint system. When I click the mouse, it finds the quickest route through set nodes from the current position to the mouse click and stores them in CurrentPath, then is meant to move the player using the code above. Thanks for the fast reply!
Current path is a stack of vector3. It has previously stored all the waypoints that give the quickest route from the players current position to the final position. It all works except for the fact it's doing it all in one frame. Coroutines didn't work although I've not tried much with them. Thanks!
Your answer
Follow this Question
Related Questions
Need help setting up Simple_Swordman by Black Hammer 0 Answers
Player won't stop moving while blocking 1 Answer
whether the player can react based on user input while following in the waypoint system? 0 Answers
Waypoint marker that counts distance from player 0 Answers
Player movement boudaries in 2D 1 Answer