44 lines
1.6 KiB
C#
44 lines
1.6 KiB
C#
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;
|
||
|
||
}
|
||
}
|
||
}
|