unity3d Vector3 Static Values


Example

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);
    }
}

A sphere inflated and deflated between Vector3.zero and Vector3.one



Static Directions

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.

In the left-handed coordinate system, the X axis moves to the right, the Y axis moves upwards and the Z axis moves further inwards.


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);
    }
}

Animated cubes moving in the static directions.



Index

ValuexyzEquivalent new Vector3() method
Vector3.zero000new Vector3(0, 0, 0)
Vector3.one111new Vector3(1, 1, 1)
Vector3.left-100new Vector3(-1, 0, 0)
Vector3.right100new Vector3(1, 0, 0)
Vector3.down0-10new Vector3(0, -1, 0)
Vector3.up010new Vector3(0, 1, 0)
Vector3.back00-1new Vector3(0, 0, -1)
Vector3.forward001new Vector3(0, 0, 1)