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;