Unity中正確使用Lerp

本文轉(zhuǎn)自blueraja
Unity官方示例中關(guān)于Lerp()函數(shù)的使用并非勻速插值,原因是Lerp()的from參數(shù)是在不斷更新的,若想實(shí)現(xiàn)勻速插值效果,應(yīng)按照類似下述方法。

using UnityEngine;
using System.Collections;
 
public class LerpOnSpacebarScript : MonoBehaviour
{
    /// <summary>
    /// The time taken to move from the start to finish positions
    /// </summary>
    public float timeTakenDuringLerp = 1f;
 
    /// <summary>
    /// How far the object should move when 'space' is pressed
    /// </summary>
    public float distanceToMove = 10;
 
    //Whether we are currently interpolating or not
    private bool _isLerping;
 
    //The start and finish positions for the interpolation
    private Vector3 _startPosition;
    private Vector3 _endPosition;
 
    //The Time.time value when we started the interpolation
    private float _timeStartedLerping;
 
    /// <summary>
    /// Called to begin the linear interpolation
    /// </summary>
    void StartLerping()
    {
        _isLerping = true;
        _timeStartedLerping = Time.time;
 
        //We set the start position to the current position, and the finish to 10 spaces in the 'forward' direction
        _startPosition = transform.position;
        _endPosition = transform.position + Vector3.forward*distanceToMove;
    }
 
    void Update()
    {
        //When the user hits the spacebar, we start lerping
        if(Input.GetKey(KeyCode.Space))
        {
            StartLerping();
        }
    }
 
    //We do the actual interpolation in FixedUpdate(), since we're dealing with a rigidbody
    void FixedUpdate()
    {
        if(_isLerping)
        {
            //We want percentage = 0.0 when Time.time = _timeStartedLerping
            //and percentage = 1.0 when Time.time = _timeStartedLerping + timeTakenDuringLerp
            //In other words, we want to know what percentage of "timeTakenDuringLerp" the value
            //"Time.time - _timeStartedLerping" is.
            float timeSinceStarted = Time.time - _timeStartedLerping;
            float percentageComplete = timeSinceStarted / timeTakenDuringLerp;
 
            //Perform the actual lerping.  Notice that the first two parameters will always be the same
            //throughout a single lerp-processs (ie. they won't change until we hit the space-bar again
            //to start another lerp)
            transform.position = Vector3.Lerp (_startPosition, _endPosition, percentageComplete);
 
            //When we've completed the lerp, we set _isLerping to false
            if(percentageComplete >= 1.0f)
            {
                _isLerping = false;
            }
        }
    }
}

最后附上Lerp()的內(nèi)部實(shí)現(xiàn):

public static Vector3 Lerp(Vector3 start, Vector3 finish, float percentage)
{
    //Make sure percentage is in the range [0.0, 1.0]
    percentage = Mathf.Clamp01(percentage);
 
    //(finish-start) is the Vector3 drawn between 'start' and 'finish'
    Vector3 startToFinish = finish - start;
 
    //Multiply it by percentage and set its origin to 'start'
    return start + startToFinish * percentage;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,816評論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • 轉(zhuǎn)載一篇高質(zhì)量博文,原地址請戳這里轉(zhuǎn)載下來方便今后查看。1 背景不能只分析源碼呀,分析的同時(shí)也要整理歸納基礎(chǔ)知識,...
    Elder閱讀 2,005評論 0 24
  • 早晨的時(shí)候,穿過這條路。陽光剛剛普照,一切都有種金光閃閃的樣子。我很想停留下來,享受這段不易得的時(shí)光...
    陽阿薤露閱讀 301評論 0 0
  • 直到今天,我才明白,好多事情,遠(yuǎn)遠(yuǎn)沒有你想的那么簡單!說好相愛的人,除了別的原因,距離是最大的阻礙!不在身邊的...
    翩翩若無閱讀 224評論 0 0

友情鏈接更多精彩內(nèi)容