Transform有關旋轉個屬性和方法測試
一,屬性
1,var eulerAngles : Vector3
- public float yRotation = 5.0F;
- void Update() {
- yRotation += Input.GetAxis("Horizontal");
- transform.eulerAngles = new Vector3(10, yRotation, 0);
- }
效果:與Quaternion.enlerAngles基本相同,用來設定物體的旋轉角度,但不要分別設置xyz,要整體賦值。
2,var rotation : Quaternion
- 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);
- transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
- }
效果:代表了物體的旋轉,不能直接為transform.rotation.eulerAngles賦值。可以使用各種Quaternion的方法。
二,方法
1,function Rotate (eulerAngles : Vector3, relativeTo : Space = Space.Self) :void
- void Update() {
- transform.Rotate(Vector3.right * Time.deltaTime);
- transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
- }
效果,使物體旋轉一個基於歐拉角的旋轉角度,eulerAngles.z度圍繞z軸,eulerAngles.x度圍繞x軸,eulerAngles.y度圍繞y軸。vector3可以變成3個分開的float值。
2,function Rotate (axis : Vector3, angle : float, relativeTo : Space = Space.Self) : void
- void Update() {
- transform.Rotate(Vector3.right, Time.deltaTime);
- transform.Rotate(Vector3.up, Time.deltaTime, Space.World);
- }
效果:按照angle度圍繞axis軸旋轉。
3,function RotateAround (point : Vector3, axis : Vector3, angle : float) : void
- public Transform target;
- Quaternion rotation;
- void Update()
- {
- transform.RotateAround(target.position, Vector3.up, 20 * Time.deltaTime);
- }
效果: 以point為中心點,以axis為軸進行旋轉,類似於圍繞某一點公轉。
4,function LookAt (target : Transform, worldUp :Vector3 = Vector3.up) : void
- public Transform target;
- void Update() {
- transform.LookAt(target);
- }
效果:使物體繞y軸旋轉,z軸一直指向target,所以顧名思義叫lookat。