Character movement with vectors- with two characters:
In the previous section we learnt what a vector is and how to move a character P (eg Pacman) from its current position to the new position.
In this section we will introduce a new character I (eg Inky). Lets say we know the Pacman’s position and Inky’s position and we want Inky to eat Pacman. So essentially we need to find a vector from Inky to Pacman. We call this vector .
It is really important to know that P comes first and I next so we get a vector from I to P.
In Unity 3d Engine we can find the direction vector using the following code snippet :
Vector3 pos = pacman.transform.position
Vector3 dir = (inky.transform.position - pacman.transform.position).normalized
//draw a line between the 2 enemys :)
Debug.DrawLine (pos, pos + dir * 10, Color.red, Mathf.Infinity);