开发日记:Unity周期自转通用脚本

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;
        }
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值