Movement without smoothing

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. :smile:

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

I can’t help that much but magnitude is the length of the vector ;).

What’s the difference between using magnitude and normalized? :anguished:

To stop the pathfinding I simply do:

path.vectorPath.Clear();

if the enemy is within attacking distance of the player, and if (path.vectorPath.Count == 0 && (player is out of range)) then I call StartPath(…) again.

When you use normalize a vector you set the magnitude of it to 1.

The code makes sure you don’t run too far or too short of a node and no more than once per turn, and lerp makes it look like you move at a constant pace. ‘accum’ is the accumulated distance along the checked nodes and distance is how far you travel can travel each update.

Hi

You can use this script: http://pastebin.com/DvjRLQLV
From this thread: Simple example script for a 2d top down character (y up axis)?