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.
var aPosition = Vector3(1, 1, 1);
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Vector3 aPosition = new Vector3(1, 1, 1);
}
import UnityEngine
import System.Collections
class example(MonoBehaviour):
public aPosition as Vector3 = Vector3(1, 1, 1)
Vector3 also defines some common values as constants.
var direction = Vector3.up; // the same as Vector3(0, 1, 0);
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Vector3 direction = Vector3.up;
}
import UnityEngine
import System.Collections
class example(MonoBehaviour):
public direction as Vector3 = Vector3.up
Operations on single vectors are accessed the following way:
SomeVector.Normalize();
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Example() {
SomeVector.Normalize();
}
}
import UnityEngine
import System.Collections
class example(MonoBehaviour):
def Example():
SomeVector.Normalize()
And operations using multiple vectors are done using Vector3 class functions:
var oneVector : Vector3 = Vector3(0,0,0);
var otherVector : Vector3 = Vector3(1,1,1);
var theDistance = Vector3.Distance(oneVector, otherVector);
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Vector3 oneVector = new Vector3(0, 0, 0);
public Vector3 otherVector = new Vector3(1, 1, 1);
public float theDistance = Vector3.Distance(oneVector, otherVector);
}
import UnityEngine
import System.Collections
class example(MonoBehaviour):
public oneVector as Vector3 = Vector3(0, 0, 0)
public otherVector as Vector3 = Vector3(1, 1, 1)
public theDistance as single = 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.