Overview: Vectors
Overview: Vectors

Unity uses the Vector3 class throughout to represent all 3D vectors. The individual components of a 3D vector can be accessed through its x, y and z member variables.

var aPosition : Vector3;
aPosition.x = 1;
aPosition.y = 1;
aPosition.z = 1;

You can also use the Vector3 constructor function to initialise all components at once.

var aPosition = Vector3(1, 1, 1);

Vector3 also defines some common values as constants.

var direction = Vector3.up; // the same as Vector3(0, 1, 0);

Operations on single vectors are accessed the following way:


And operations using multiple vectors are done using Vector3 class functions:

theDistance = Vector3.Distance(oneVector, otherVector);

(Note that you have to write Vector3. in front of the function name to tell Javascript where to find the function. This applies to all class functions.)

You can also use the common math operators to manipulate vectors:

combined = vector1 + vector2;

See the documentation on the Vector3 class for the full list of operations and properties available.