靜態值

Vector3 結構包含一些提供常用 Vector3 值的靜態變數。大多數代表一個方向,但它們仍然可以創造性地用於提供額外的功能。

Vector3.zeroVector3.one

Vector3.zeroVector3.one 通常用於標準化的 Vector3; 也就是說,Vector3,其中 xyz 值的大小為 1.因此,Vector3.zero 代表最低值,而 Vector3.one 代表最大值。

Vector3.zero 也常用於設定物件變換的預設位置。

以下類使用 Vector3.zeroVector3.one 來膨脹和收縮球體。

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

在 Vector3.zero 和 Vector3.one 之間膨脹和收縮的球體

靜態方向

靜態方向在許多應用中都是有用的,沿著所有三個軸的正負方向。值得注意的是,Unity 採用左手座標系統,它對方向有影響。

在左手座標系中,X 軸向右移動,Y 軸向上移動,Z 軸向內移動。

以下類使用靜態 Vector3 方向沿三個軸移動物件。

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

動畫的立方體在靜態方向移動。

指數

X ÿ ž 等效的 new Vector3() 方法
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)