Unity中通过协程实现简单物体移动旋转缩放渐变动画

IEnumerator CoroutineFadeTo(AnimationType animationType, Transform trans, Vector3 endValueS, float timeS,float timeB)
    {
        Vector3 endValue=endValueS;
        Vector3 startValue = Vector3.zero;
        Vector3 startValueS = Vector3.zero;
        bool isEndValue = false;
        float time = 0;

        switch (animationType)
        {
            case AnimationType.Move:
                startValue = trans.position;
                break;
            case AnimationType.LocalMove:
                startValue = trans.localPosition;
                break;
            case AnimationType.Rotete:
                startValue = trans.eulerAngles;
                break;
            case AnimationType.LocalRotate:
                startValue = trans.localEulerAngles;
                break;
            case AnimationType.Scale:
                startValue = trans.localScale;
                break;
            default:
                break;
        }
        startValueS = startValue;
        p: ;
        float dis=Vector3.Distance(startValue, endValue); ;
        switch (animationType)
        {
            case AnimationType.Move:
                dis = Vector3.Distance(trans.position, endValue);
                break;
            case AnimationType.LocalMove:
                dis = Vector3.Distance(trans.localPosition, endValue);
                break;
            case AnimationType.Rotete:
                dis = Vector3.Distance(trans.eulerAngles, endValue);
                break;
            case AnimationType.LocalRotate:
                dis = Vector3.Distance(trans.localEulerAngles, endValue);
                break;
            case AnimationType.Scale:
                dis = Vector3.Distance(trans.localScale, endValue);
                break;
            default:
                break;
        }
        
        if (dis <= 0.001f)
        {
            if (isEndValue)
            {
                endValue = endValueS;
                isEndValue = false;
                time = timeB;
            }
            else {
                endValue = startValue;
                isEndValue = true;
                time = timeS;
            }
            goto p;
        }
        if (time==0)
        {
            time = timeS;
        }
        //posOffset
        float xoffset = (endValue.x - startValue.x) / (time / 0.02f);
        float yoffset = (endValue.y - startValue.y) / (time / 0.02f);
        float zoffset = (endValue.z - startValue.z) / (time / 0.02f);

        while (true)
        {
            yield return new WaitForFixedUpdate();
            switch (animationType)
            {
                case AnimationType.Move:
                    trans.position += new Vector3(xoffset, yoffset, zoffset);
                    if (Vector3.Distance(trans.position,endValue)<0.01f)
                        time = -1;
                    break;
                case AnimationType.LocalMove:
                    trans.localPosition += new Vector3(xoffset, yoffset, zoffset);
                    if (Vector3.Distance(trans.localPosition, endValue) < 0.01f)
                        time = -1;
                    break;
                case AnimationType.Rotete:
                    trans.eulerAngles += new Vector3(xoffset, yoffset, zoffset);
                    if (Vector3.Distance(trans.eulerAngles, endValue) < 0.01f)
                        time = -1;
                    break;
                case AnimationType.LocalRotate:
                    trans.localEulerAngles += new Vector3(xoffset, yoffset, zoffset);
                    if (Vector3.Distance(trans.localEulerAngles, endValue) < 0.01f)
                        time = -1;
                    break;
                case AnimationType.Scale:
                    trans.localScale += new Vector3(xoffset, yoffset, zoffset);
                    if (Vector3.Distance(trans.localScale, endValue) < 0.01f)
                        time = -1;
                    break;
                default:
                    break;
            }
            if (time <= 0)
            {
                if (isEndValue)
                {
                    endValue = endValueS;
                    startValue = startValueS;
                    isEndValue = false;
                    time = timeB;
                    audioFade.Play();
                }
                else
                {
                    endValue = startValue;
                    startValue = endValueS;
                    isEndValue = true;
                    time = timeS;
                }
                goto p;
            }
        }
    }

此段代码主要是实现简单的pingpong类型的渐变动画

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值