世界空间中的旋转变换,以 Quaternion 形式存储。
Unity 在内部将旋转存储为四元数。要旋转对象,请使用 Transform.Rotate。
要将旋转设置为欧拉角,请使用 Transform.eulerAngles。Transform.rotation 将提供或接受使用 Quaternion 的旋转。
In the example below rotation obtains the orientation of the cube. The cube is rotated left-to-right and forward-to-back. Create a project with a single cube at the origin. Attach this script to the cube.
using UnityEngine;
// Tilt the cube using the arrow keys. When the arrow keys are released // the cube will be rotated back to the center using Slerp.
public class ExampleScript : MonoBehaviour { float smooth = 5.0f; float tiltAngle = 60.0f;
void Update() { // Smoothly tilts a transform towards a target rotation. float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle; float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
// Dampen towards the target rotation transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth); } }