When distance becomes less than zero?

when the Player runs towards the node the distance between them gradually decreases until it reaches the node or, in the this case ,til it gets close enough to pick another node. so why the script checks if the distance gets below zero?

void MoveToward()
	{
		//when DebugMode is enabled 
		if (DebugMode)
		{
			//Draw a white line from the first node to the last node of the path
			for (int i=0; i<path.Count-1; ++i)
			{
				//  draw a line         from the first   towards the next    color white  for 0.01 sec
				Debug.DrawLine((Vector3)path*, (Vector3)path[i+1], Color.white, 0.01f);*
  •  	}*
    
  •  }*
    
  •  //cache our position* 
    
  •  Vector3 myPos = transform.position;*
    
  •  //calculate our xdistance from the node*
    
  •  float Xdistance = myPos.x - currNode.x;*
    

_ if (Xdistance < 0) Xdistance -= Xdistance*2;_

  •  //calculate our zdistance from the node*
    
  •  float Ydistance = myPos.z - currNode.z;*
    
  •  //walk that distance in z*
    

_ if (Ydistance < 0) Ydistance -= Ydistance*2;_

  •  // if we get close enough to a node      and  if this node is the target* 
    
  •  if ((Xdistance < 0.1 && Ydistance < 0.1) && m_target == currNode) //Reached target*
    
  •  {*
    
  •  	ChangeState(State.IDLE);*
    
  •  } // but if we get close enough but it is not the target* 
    
  •  else if (Xdistance < 0.1 && Ydistance < 0.1)*
    
  •  {*
    

_ //** switch to target the next waypoint in the path **//_

  •  	nodeIndex++; //pick another node*
    
  •  	//reached the current node* 
    
  •  	onNode = true;*
    
  •  }*
    

_ /Move toward waypoint/_

  •  // currNode-myPos is the direction we want to head (this will yield a magnitude too)*
    
  •  Vector3 motion = currNode - myPos;*
    
  •  //By normalizing it we make the magnitude be 1 unit long*
    
  •  motion.Normalize();*
    
  •  //then we add a direction(motion) and our custom magnitude (m_speed)* 
    

myPos += motion * m_speed;

  •  //finally apply to the character*
    
  •  transform.position = myPos;*
    
  • }*
    I want to know why it checks this part ==>
    if (Xdistance < 0) Xdistance -= Xdistance*2;
    if (Ydistance < 0) Ydistance -= Ydistance*2;

The code you are asking about is workable, but very strange. While not exactly identical, you can replace lines 13 - 36 with:

    if (Vector3.Distance(myPos,currNode) < 0.1f) {
         if (m_target == currNode) {
             ChangeState(State.IDLE);
         }
         else {
             nodeIndex++; //pick another node
             onNode = true;
         }
     }

The code you are asking about I believe is trying to generating a positive value for the x and y distance from the destination. For example let’s say that the x position of the waypoint is 3.95, but your object is at 4.0. This calculation:

 float Xdistance = myPos.x - currNode.x;

…would produce a negative value. But the problem is that this calculation:

Xdistance -= Xdistance * 2

…should be:

Xdistance += Xdistance * 2;

…or much better yet:

Xdistance = -Xdistance;

…but all this calculation is unnecessary since Vector3.Distance() will produce a positive distance.

Also, the code as is will not compile, so I’m not sure where you got it.