rotateangle数组 代表完成一次旋转度数,等待冷却时间,再进行下一次度数,如果数组里的旋转度数都执行完毕,再从头开始,
代码:
public enum OperaAxle
{
X,Y,Z
}
public class FixFreeRotate : MonoBehaviour
{
[Header("是否顺时针")] public bool clockwise;
[Header("世界/局部")] public Space rotateSpace;
[Header("旋转轴")] public OperaAxle curOperaAxle;
[Header("冷却时间")] public float coldTime;
[Header("旋转速度")] public float rotateSpeed;
[Header("旋转循环度数")] public float[] rotateAngle;
private Vector3 rotateAxle;
private int factor=1;
private Quaternion initRt;
private Quaternion rotation;
private float rotateValue=0;
private int index = 0;
private float targetValue=0;
private bool isCold = false;
private void Start()
{
//定义旋转系数
if (clockwise)
{
factor = 1;
}
else
{
factor = -1;
}
//判断是世界坐标系还是局部坐标系
//确定旋转轴
if (rotateSpace==Space.World)
{
if (curOperaAxle == OperaAxle.X)
{
rotateAxle = Vector3.right;
}
else if (curOperaAxle == OperaAxle.Y)
{
rotateAxle = Vector3.up;
}
else
{
rotateAxle = Vector3.forward;
}
}
else
{
if (curOperaAxle == OperaAxle.X)
{
rotateAxle = transform.right;
}
else if (curOperaAxle == OperaAxle.Y)
{
rotateAxle = transform.up;
}
else
{
rotateAxle = transform.forward;
}
}
initRt = transform.localRotation;
targetValue = rotateValue+rotateAngle[index];
}
private float curTime = 0;
private void Update()
{
//在冷却状态下
if (isCold)
{
curTime += Time.deltaTime;
if (curTime > coldTime)
{
curTime = 0;
isCold = false;
index++;
if (index >= rotateAngle.Length)
{
index = 0;
}
targetValue += rotateAngle[index];
}
}
//不在冷却状态下
else
{
rotateValue += Time.deltaTime * rotateSpeed;
if (rotateValue > targetValue)
{
rotateValue -= Time.deltaTime * rotateSpeed;
isCold = true;
return;
}
rotation = Quaternion.AngleAxis(rotateValue*factor,rotateAxle)*initRt;
transform.rotation = rotation;
}
}
}