The Vector3
structure contains some static variables that provide commonly used Vector3
values. Most represent a direction, but they can still be used creatively to provide additional functionality.
Vector3.zero
and Vector3.one
Vector3.zero
and Vector3.one
are typically used in connection to a normalised Vector3
; that is, a Vector3
where the x
, y
and z
values have a magnitude of 1. As such, Vector3.zero
represents the lowest value, whilst Vector3.one
represents the largest value.
Vector3.zero
is also commonly used to set the default position on object transforms.
The following class uses Vector3.zero
and Vector3.one
to inflate and deflate a sphere.
using UnityEngine;
public class Inflater : MonoBehaviour
{
<summary>A sphere set up to inflate and deflate between two values.</summary>
public ScaleBetween sphere;
///<summary>On start, set the sphere GameObject up to inflate
/// and deflate to the corresponding values.</summary>
void Start()
{
// Vector3.zero = Vector3(0, 0, 0); Vector3.one = Vector3(1, 1, 1);
sphere.SetScale(Vector3.zero, Vector3.one);
}
}
The static directions can be useful in a number of applications, with direction along the positive and negative of all three axis. It is important to note that Unity employs a left-handed coordinate system, which has an affect on direction.
The following class uses the static Vector3
directions to move objects along the three axis.
using UnityEngine;
public class StaticMover : MonoBehaviour
{
<summary>GameObjects set up to move back and forth between two directions.</summary>
public MoveBetween xMovement, yMovement, zMovement;
///<summary>On start, set each MoveBetween GameObject up to move
/// in the corresponding direction(s).</summary>
void Start()
{
// Vector3.left = Vector3(-1, 0, 0); Vector3.right = Vector3(1, 0, 0);
xMovement.SetDirections(Vector3.left, Vector3.right);
// Vector3.down = Vector3(0, -1, 0); Vector3.up = Vector3(0, 0, 1);
yMovement.SetDirections(Vector3.down, Vector3.up);
// Vector3.back = Vector3(0, 0, -1); Vector3.forward = Vector3(0, 0, 1);
zMovement.SetDirections(Vector3.back, Vector3.forward);
}
}
Value | x | y | z | Equivalent new Vector3() method |
---|---|---|---|---|
Vector3.zero | 0 | 0 | 0 | new Vector3(0, 0, 0) |
Vector3.one | 1 | 1 | 1 | new Vector3(1, 1, 1) |
Vector3.left | -1 | 0 | 0 | new Vector3(-1, 0, 0) |
Vector3.right | 1 | 0 | 0 | new Vector3(1, 0, 0) |
Vector3.down | 0 | -1 | 0 | new Vector3(0, -1, 0) |
Vector3.up | 0 | 1 | 0 | new Vector3(0, 1, 0) |
Vector3.back | 0 | 0 | -1 | new Vector3(0, 0, -1) |
Vector3.forward | 0 | 0 | 1 | new Vector3(0, 0, 1) |