静态值
Vector3
结构包含一些提供常用 Vector3
值的静态变量。大多数代表一个方向,但它们仍然可以创造性地用于提供额外的功能。
Vector3.zero
和 Vector3.one
Vector3.zero
和 Vector3.one
通常用于标准化的 Vector3
; 也就是说,Vector3
,其中 x
,y
和 z
值的大小为 1.因此,Vector3.zero
代表最低值,而 Vector3.one
代表最大值。
Vector3.zero
也常用于设置对象变换的默认位置。
以下类使用 Vector3.zero
和 Vector3.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);
}
}
静态方向
静态方向在许多应用中都是有用的,沿着所有三个轴的正负方向。值得注意的是,Unity 采用左手坐标系统,它对方向有影响。
以下类使用静态 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) |