Unity Lerp は、高速から低速への現在の比率ではなく、固定のステップ サイズで近似的に一定の遷移を実装します

アイデア:

Lerp 関数は、例として Vector3 を取ります。

Vector3.Lerp(Pos1,Pos2,LerpStep)

Pos1=開始位置 (通常、transform.positionon、つまり、各フレーム オブジェクトのリアルタイムの位置。UI オブジェクトの場合、次のように RectTransform です)

Pos2=終了位置

LerpStep=毎回 Lerp 関数を呼び出して完了する割合

例えば:

オブジェクトを (0,0,0) に置きます

Vector3.Lerp(transform.position,new Vector3(10,0,0),0.1f)

この関数が初めて呼び出されたとき、オブジェクトの X 座標は 0+(10-0)*0.1=1 です。

最初の位置は (1,0,0) になります

2回目: (1+(10-1)*0.1,0,0)=(1.9,0,0)

3回目: (1.9+(10-1.9)*0.1,0,0)=(2.71,0,0)

したがって、均一な速度を実現するには、プロセスに応じてステップ サイズを変更する必要があります。

均一な速度を達成するための例として、上記の例を引き続き使用します。

t= 10/(10-transform.position.x)*0.1;//t=総距離/(総距離-現在の距離)*ステップ長

Vector3.Lerp(transform.position,new Vector3(10,0,0),t)

この関数が初めて呼び出されたとき (t=1*0.1)、オブジェクトの X 座標は 0+(10-0)*0.1=1 です。

最初の位置は (1,0,0) になります

2 回目、t=10/(10-1)*0.1=(10/9)*0.1、オブジェクトの X 座標: 1+(10-1)*(10/9)*0.1=2

2 番目の位置は (2,0,0) になります

3 回目: t=10/(10-2)*0.1=(10/8)*0.1、オブジェクトの X 座標: 2+(10-2)*(10/8)*0.1=3

など(個人的な考えで、必ずしも正しいとは限りません)

以下は、UI の動きを実装するための私のコードです。

public class ReadBook : MonoBehaviour
{     public RectTransform Page1;     public TMPro.TextMeshProUGUI Page1Text;     public RectTransform Page2;     public TMPro.TextMeshProUGUI Page2Text;



    プライベートブールはLerpです。

    float t;
    float OriginalLength;
    float currentLength;
    public float LerpStep=0.1f;//毎回 10% 完了し、Time.deltaTime を掛けた値、つまり、合計完了時間 = 1/0.1=10 秒 public RectTransform

    LastPage;


    // Start は最初のフレーム更新の前に呼び出されます
    void Start()
    {         OriginalLength = (Page2.localPosition - LastPage.localPosition).magnitude;         Debug.Log("原长是:" + OriginalLength);     }


    // Update はフレームごとに 1 回呼び出されます
    void Update()
    {

        if (isLerp)
        {             t = OriginalLength / currentLength * LerpStep*Time.deltaTime;
            

            Page2.localPosition = Vector3.Lerp(Page2.localPosition, LastPage.localPosition, t);


            if (Page2.localPosition== LastPage.localPosition)
            {

Debug.Log                 ("Lerp位置が終了しました");
                isLerp = false;
            }
       
        }
    }

おすすめ

転載: blog.csdn.net/qianhang120/article/details/128544762
おすすめ