2025-09-08 17:37:12 +08:00

44 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
namespace DongWuYiXue.DaoNiaoShu
{
public class Rotator : MonoBehaviour
{
public Transform pointToRotateAround; // 要围绕旋转的点
public Vector3 rotationAxis = Vector3.up; // 围绕的旋转轴默认是y轴
public float minRotationAngle = 0f; // 最小旋转角度
public float maxRotationAngle = 360f; // 最大旋转角度
private float currentRotationAngle; // 当前旋转角度
// 枚举类型来设置旋转方向
public enum RotationDirection
{
Clockwise,
CounterClockwise
}
public RotationDirection rotationDirection = RotationDirection.Clockwise; // 旋转方向,默认顺时针
//void Update()
//{
// // 在这里你可以动态传入一个角度来改变旋转角度
// if (Input.GetKeyDown(KeyCode.Space))
// {
// SetRotationAngle(Random.Range(minRotationAngle, maxRotationAngle));
// }
//}
// 设置旋转角度的方法
public void SetRotationAngle(float angle)
{
// 限制角度范围
angle = Mathf.Clamp(angle, minRotationAngle, maxRotationAngle);
// 得到旋转方向的因子
float rotationFactor = (rotationDirection == RotationDirection.Clockwise) ? 1f : -1f;
// 计算角度差
float angleDelta = angle - currentRotationAngle;
// 将角度差应用到物体上
transform.RotateAround(pointToRotateAround.position, rotationAxis, angleDelta * rotationFactor);
// 更新当前旋转角度
currentRotationAngle = angle;
}
}
}