Overview: Vectors 向量

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;

Unity使用 Vector3 类来表现所有的3D向量.我们可以通过他的x,y和z成员变量获得3D向量的组件.

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.

你也能使用 Vector3 构造函数初始化所有的组件.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public Vector3 aPosition = new Vector3(1, 1, 1);
}
var aPosition = Vector3(1, 1, 1);

Vector3 also defines some common values as constants.

Vector3 也定义了一些固定的常用值.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public Vector3 direction = Vector3.up;
}	

var direction = Vector3.up ; 

// 等同于 Vector3(0, 1, 0)

Operations on single vectors are accessed the following way:

规范化向量可以使用下面的方法:

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public void Awake() {
		SomeVector.Normalize();
	}
}
SomeVector.Normalize();

And operations using multiple vectors are done using Vector3 class functions:

使用 Vector3 类函数计算多个向量的情况:

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);
}
var oneVector : Vector3 = Vector3(0,0,0);
var otherVector : Vector3 = Vector3(1,1,1);

var theDistance = 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.

(注意你必须在函数名之前写上Vector3.以便Javascript能找到那个函数.这个规则也应用于所有的类函数.)

你也能使用常见的数学运算操作向量:combined = vector1 + vector2;

查看文档 Vector3 类可以获得更过相关信息.

最后修改:2010年11月30日 Tuesday 23:08

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。