欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

自定义的插值公式

程序员文章站 2022-04-21 10:14:37
自定义的插值公式1using System.Collections;using System.Collections.Generic;using UnityEngine;namespace ACTBook{ /** * static function Repeat (t : float, length : float) : float * 循环数值t,0到length之间。t值永远不会大于length的值,也永远不会小于0。 * 这是类似于 模 运算符,但...

自定义的插值公式

1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ACTBook
{
    /**
     * static function Repeat (t : float, length : float) : float
     * 循环数值t,0到length之间。t值永远不会大于length的值,也永远不会小于0。
     * 这是类似于 模 运算符,但可以使用浮点数。
     */
    public class Tween1 : MonoBehaviour
    {
        const float TIME = 3f;
        const float DISTANCE = 3f;


        void Update()
        {
            var t = Mathf.Repeat(Time.time, TIME) / TIME;
            Debug.Log(t);
            /**
             * static functionLerp (from : float, to : float, t : float) : float
             * 基于浮点数t返回a到b之间的插值,t限制在0~1之间。
             * 当t = 0返回from,当t = 1 返回to。当t = 0.5 返回from和to的平均值
             */
            transform.localPosition = Vector3.Lerp(new Vector3(0f, 0f, 0f), new Vector3(DISTANCE, 0f, 0f), t);
        }
    }
}

2

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ACTBook
{
    public class Tween2 : MonoBehaviour
    {
        const float TIME = 3f;
        const float DISTANCE = 3f;
        const float SMOOTH_TIME = 0.2f;

        Vector3 mVelocity;


        void Update()
        {
            var t = Mathf.Repeat(Time.time, TIME) / TIME;
            var target = Vector3.Lerp(new Vector3(0f, 0f, 0f), new Vector3(DISTANCE, 0f, 0f), t);
            /**
             * public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed = Mathf.Infinity, float deltaTime = Time.deltaTime);
             * 
             * current	The current position.
             * target	The position we are trying to reach.
             * currentVelocity	The current velocity, this value is modified by the function every time you call it.
             * smoothTime	Approximately the time it will take to reach the target. A smaller value will reach the target faster.
             * maxSpeed	Optionally allows you to clamp the maximum speed.
             * deltaTime	The time since the last call to this function. By default Time.deltaTime.
             * 
             */
            transform.localPosition = Vector3.SmoothDamp(transform.localPosition, target, ref mVelocity, SMOOTH_TIME);
        }
    }
}

3

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ACTBook
{
    public class Tween3 : MonoBehaviour
    {
        const float TIME = 3f;
        const float DISTANCE = 3f;
        const float SMOOTH_TIME = 0.2f;

        Vector3 mVelocity;


        void Update()
        {
            var t = Mathf.Repeat(Time.time, TIME) / TIME;
            var quicken_t = t * t;
            transform.localPosition = Vector3.Lerp(new Vector3(0f, 0f, 0f), new Vector3(DISTANCE, 0f, 0f), quicken_t);
        }
    }
}

4

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ACTBook
{
    public class Tween4 : MonoBehaviour
    {
        const float TIME = 3f;
        const float DISTANCE = 3f;


        void Update()
        {
            var t = Mathf.Repeat(Time.time, TIME) / TIME;
            t = (t - 1f) * (t - 1f) * (t - 1f) + 1f;
            t = t * t;
            transform.localPosition = Vector3.Lerp(new Vector3(0f, 0f, 0f), new Vector3(DISTANCE, 0f, 0f), t);
        }
    }
}

本文地址:https://blog.csdn.net/qq_36382679/article/details/107879630

相关标签: unity3D unity