Unity旋转指定角度
如果我们想要将旋转角度固定在某个特定的值,可以使用Transform.rotation
来实现。我们可以将其直接设置为Quaternion.Euler(x,y,z)
,这样就可以将物体旋转到指定的角度。
void Update() {
transform.rotation = Quaternion.Euler(45, 90, 0);
}
上面的代码将会使物体固定在45度、90度、0度的角度。
Unity物体旋转到指定角度
如果我们想要将物体沿着某个轴旋转到指定角度,可以使用Transform.Rotate
函数。这个函数可以让物体旋转一个角度,我们可以将Vector3
传递给该函数,Vector3
的值就是旋转的角度。
void Update() {
transform.Rotate(Vector3.up * 90 * Time.deltaTime);
}
上述代码将会使物体沿着y
轴旋转90度。
Unity旋转角度捕捉
Unity内置了一些机制,可以让我们快速捕捉摄像机和物体的旋转角度。我们可以使用以下函数:
transform.eulerAngles
: 这个函数返回一个物体的欧拉角旋转值。transform.localEulerAngles
: 这个函数返回一个物体的相对局部欧拉角旋转值。 使用这些函数可以方便地获取物体旋转的角度值。
Unity物体旋转角度
如果我们想要控制旋转角度的精度和细节,可以使用Quaternion.AngleAxis()
函数。该函数使用一个Vector3
表示旋转的轴,以及一个浮点数表示旋转的角度。下面的代码将物体绕着y
轴旋转45度:
void Update() {
float angle = 45f;
Vector3 axis = Vector3.up;
transform.rotation *= Quaternion.AngleAxis(angle, axis);
}
Unity限制旋转角度
对于某些游戏,需要限制物体旋转的角度。我们可以通过以下方式实现:
public float rotationSpeed = 100f;
public float maxRotationAngle = 45f;
void Update() {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
transform.Rotate(Vector3.up * h * rotationSpeed * Time.deltaTime);
float currentXRotation = transform.eulerAngles.x;
if (currentXRotation > 180) currentXRotation -= 360;
currentXRotation = Mathf.Clamp(currentXRotation - (v * rotationSpeed * Time.deltaTime), -maxRotationAngle, maxRotationAngle);
transform.rotation = Quaternion.Euler(currentXRotation, transform.eulerAngles.y, transform.eulerAngles.z);
}
上述代码可以限制物体在x
轴上最大旋转角度为45度。
Unity镜头旋转角度
对于第一人称视角的游戏,我们需要控制相机的旋转。可以使用以下代码实现:
public float rotationSensitivity = 1f;
public Transform player;
void Update() {
float h = Input.GetAxis("Mouse X") * rotationSensitivity;
float v = Input.GetAxis("Mouse Y") * rotationSensitivity;
player.Rotate(Vector3.up * h);
transform.Rotate(Vector3.left * v);
}
上述代码可以使用鼠标来控制相机的旋转。
Unity向量旋转角度
如果我们想要旋转一个3D向量,则可以使用Quaternion
旋转实例的MultiplyPoint3x4()
函数。该函数可以将向量旋转到指定的角度
Vector3 direction = new Vector3(0, 1, 0);
Quaternion rotation = Quaternion.Euler(45, 0, 0);
Vector3 rotatedDirection = rotation * direction;
上述代码将会使direction
向量绕着x
轴顺时针旋转45度。
Unity摄像机旋转角度
如果我们想要在实时更新摄像机的位置和方向,可以使用以下代码:
public Transform followTransform;
public Vector3 offset;
public float rotationSensitivity = 1f;
private float currentXRotation = 0f;
private float currentYRotation = 0f;
void Update() {
currentXRotation += Input.GetAxis("Mouse X") * rotationSensitivity;
currentYRotation += Input.GetAxis("Mouse Y") * rotationSensitivity;
currentYRotation = Mathf.Clamp(currentYRotation, -80, 80);
Quaternion rotation = Quaternion.Euler(currentYRotation, currentXRotation, 0);
Vector3 position = followTransform.position - (rotation * offset);
transform.rotation = rotation;
transform.position = position;
}
上述代码将会使相机围绕着一个物体旋转。
Unity旋转角度范围限制
如果我们想要在一定范围内限制物体旋转角度,可以使用以下代码:
public float maxRotation = 30f;
void Update() {
float xRotation = Input.GetAxis("Horizontal") * -maxRotation;
float yRotation = Input.GetAxis("Vertical") * maxRotation;
xRotation = Mathf.Clamp(xRotation, -maxRotation, maxRotation);
yRotation = Mathf.Clamp(yRotation, -maxRotation, maxRotation);
transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0);
}
上述代码可以限制物体在x
和y
轴上最大旋转角度为30度。