转:http://blog.csdn.net/treepulse/article/details/49281295
Transfrom.eulerAngles
public float yRotation = 5.0F;
void Update() {
yRotation += Input.GetAxis("Horizontal");
transform.eulerAngles = new Vector3(10, yRotation, 0);
//x轴和z轴的旋转角度始终不变,只改变绕y轴的旋转角度
}
- 1
- 2
- 3
- 4
- 5
- 6
Transfrom.rotation
//重设世界的旋转角度
transform.rotation = Quaternion.identity;
- 1
- 2
//平滑倾斜物体向一个target旋转
public float smooth = 2.0F;
public float tiltAngle = 30.0F;
void Update() {
float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
//向target旋转阻尼
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Transfrom.RotateAround
//围绕世界坐标原点,每秒20度物体自旋
transform.RotateAround (Vector3.zero, Vector3.up, 20 * Time.deltaTime);
- 1
- 2
Transfrom.Rotate
//沿着x轴每秒1度慢慢的旋转物体
transform.Rotate(Vector3.right * Time.deltaTime);
//相对于世界坐标沿着y轴每秒1度慢慢的旋转物体
transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
- 1
- 2
- 3
- 4
Transfrom.LookAt
当该物体设置了LookAt并指定了目标物体时,该物体的z轴将始终指向目标物体,在设置了worldUp轴向时,该物体在更接近指定的轴向时旋转便的灵活,注意worldUp指的是世界空间,不论你物体在什么位置,只要接近指定的轴方向,旋转会变的更灵活。
//这个完成的脚本附加到一个摄像机,使它连续指向另一个物体。
//target变量作为一个属性显示在检视面板上
//拖动另一个物体到这个上面,是摄像机注视它
public Transform target;
//每帧旋转摄像机,这样它保持注视目标
void Update() {
transform.LookAt(target);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
// Point the object at the world origin
//使物体注视世界坐标原点
transform.LookAt(Vector3.zero);
- 1
- 2
- 3
Quaternion.LookRotation
//大多数时间可以使用transform.LookAt代替
public Transform target;
void Update() {
Vector3 relativePos = target.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
transform.rotation = rotation;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Quaternion.Angle
//计算transform和target之间的旋转角度
public Transform target;
void Update() {
float angle = Quaternion.Angle(transform.rotation, target.rotation);
}
- 1
- 2
- 3
- 4
- 5
Quaternion.Eulr
//绕y轴旋转30度
public Quaternion rotation = Quaternion.Euler(0, 30, 0);
- 1
- 2
Quaternion.Slerp
//在from和to之间插值旋转.
//(from和to不能与附加脚本的物体相同)
public Transform from;
public Transform to;
public float speed = 0.1F;
void Update() {
transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, Time.time * speed);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
其实任何角度都可以没有限制
Quaternion.FromToRotation
//设置旋转,变换的y轴转向z轴
transform.rotation = Quaternion.FromToRotation (Vector3.up, transform.forward);
- 1
- 2