Unity – 2D Vector Math

2D Vector Math uses Pythagoras’s Theorem.
It has 2 components, x and y, that rapresent the point from origin 0,0 to any point on a 2D plane.

A vector has:
– Magnitude, it is the lenght of the vector
– Direction, it is the direction of movement

Below an example to calculate the distance between 2 characters

unity-vector-math-img-001

unity-vector-math-img-002

Vector Math can be used to predict position of moving objects.
Below the character has a velocity o 12,5 per hour, it means it moves 12 units in x and 5 units in y per hour.
You can calculate the final position after one hour with vector math:

unity-vector-math-img-003

Unity has vector math functions.

Official guide at: http://docs.unity3d.com/Documentation/ScriptReference/Vector2.html

Calculate distance between two objects

var distance = Vector2.Distance(object1.transform.position, object2.transform.position);

Statement: Vector2.Distance(a,b) is the same as (a-b).magnitude.