Unity学习记录-Vector和Transform
常用的Vector向量

Vector常用方法
1 2 3
| Vector3 v1 = new Vector3(1, 1, 0); Vector3 v2 = new Vector3(0, 0, 1); Vector3.Distance(v1, v2);
|
Transform位置相关
1
| print(this.transform.position);
|
1
| print(this.transform.localPosition);
|
1
| print(this.transform.forward);
|
1
| print(this.transform.up);
|
1
| print(this.transform.right);
|
位移和旋转的计算
位移的计算
-
自己计算
1 2
| float speed = 1f; this.transform.position += this.transform.forward * speed * Time.deltaTime;
|
-
利用API计算
- 参数一:表示位移速度
- 参数二:表示位移的相对坐标系,默认为自身坐标系
1 2 3 4 5
| this.transform.Translate(Vector3.forward * speed * Time.deltaTime,Space.World);
this.transform.Translate(this.transform.forward * speed * Time.deltaTime, Space.World);
this.transform.Translate(Vector3.forward*speed*Time.deltaTime, Space.Self);
|
注意:不要这样写:
1 2
| this.transform.Translate(this.transform.forward * speed * Time.deltaTime, Space.Self);
|
旋转的计算
自转
1
| this.transform.Rotate(new Vector3(0, 10, 0) * Time.deltaTime, Space.Self);
|
公转
- 第一个参数:相对于哪个点公转
- 第二个参数:相对于哪一个轴公转
- 第三个参数:速度
1
| this.transform.RotateAround(Vector3.zero, Vector3.up, 10 * Time.deltaTime);
|
绕 0,1,0 公转
示例代码:模拟地球运动
1 2 3 4 5
| void Update() { this.transform.Rotate(new Vector3(0, 10, 0) * Time.deltaTime, Space.Self); this.transform.RotateAround(Vector3.zero, new Vector3(0,1,-1), 10 * Time.deltaTime); }
|
效果:
