Hello!
I’m trying to make my player move along the path precisely. I saw this thread (The thread) but I can’t understand what you mean with “accum”, “magnitude”, “d” and all of that stuff. Where is the direction? Could someone rewrite it in pseudo code or something? The code is working and doing like I want it to do, but I just wanna know HOW it works.
One more question, how can I stop the pathfinding? Like, when a enemy is close to the player I want it to stop some units away from him and start attacking.
Edit: This code doesnt seem to work if I update the path at runtime. Any fix?
float distance = 0;
float speed = 1;
public void Update ()
{
List<Vector3> points = new List<Vector3> ();
points = path.vectorPath;
distance += speed * Time.deltaTime;
float accum = 0;
for ( int i = 0; i < points.Count-1; i++ )
{
float d = (points[i+1] - points[i]).magnitude;
if ( accum + d >= distance )
{
transform.position = Vector3.Lerp ( points[i], points[i+1], (distance - accum)/d);
return;
}
accum += d;
}