Overview: Vectors Manual     Reference     Scripting  
Scripting
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. Note: This only works on JavaScript and Boo. In C# you will have to create a new instance of a Vector and assign it. 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.

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

Vector3 also defines some common values as constants.

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

Operations on single vectors are accessed the following way:

JavaScript
SomeVector.Normalize();

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

JavaScript
var oneVector : Vector3 = Vector3(0,0,0);
var otherVector : Vector3 = Vector3(1,1,1);

var 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.