为什么我的图像位置以两种不同的速度缓慢移动?

Why Are My Image Positions Lerping At Two Different Speeds?

我这里有一些简单的代码。我遇到的问题是图像收缩到中心(本地位置 Vector3.zero),当它们离开中心时,它们的速度似乎几乎是原来的两倍。我相信我正在使用相同的逻辑来移动它们,所以我想知道是否有人可以告诉我是什么导致了这种行为。

也欢迎任何关于使代码本身更好的建议,但我主要关心的是插值的速度。我可能应该通过参数而不是总时间传递当前时间,但你知道......我并不是每次都在第一次做出完美的决定。

问题中的具体行:

IEnumerator ContractImages(float totalTime)
{
    yield return m_WaitForEndOfFrame;

    m_CurrentContractionTime += Time.deltaTime;

    float t =  m_CurrentContractionTime / totalTime;

    Debug.Log(string.Format("Expansion Rate Factor: {0}", t));
    Debug.Log(string.Format("Current Expansion Time: {0}", m_CurrentContractionTime));

    for (int i = 0; i < m_MovingImages.Length; i++)
    {
        var r = m_MovingImages[i].GetComponent<RectTransform>(); //Optimize

        r.localPosition = Vector3.Lerp(r.localPosition, Vector3.zero, t);
    }

    if (m_CurrentContractionTime < totalTime)
        m_ContractionRoutine = StartCoroutine(ContractImages(totalTime));

    else if (m_ContractionRoutine != null)
    {
        StopCoroutine(m_ContractionRoutine);
        InvokeContractionComplete();
        m_CurrentContractionTime = 0;
    }
}

IEnumerator ExpandImages(float totalTime)
{
    yield return m_WaitForEndOfFrame;

    m_CurrentExpansionTime += Time.deltaTime;

    float t = m_CurrentExpansionTime / totalTime;

    Debug.Log(string.Format("Expansion Rate Factor: {0}", t));
    Debug.Log(string.Format("Current Expansion Time: {0}", m_CurrentExpansionTime));

    for (int i = 0; i < m_MovingImages.Length; i++)
    {
        var r = m_MovingImages[i].GetComponent<RectTransform>(); //Optimize

        r.localPosition = Vector3.Lerp(Vector3.zero, m_StartingVectors[i], t);
    }

    if (m_CurrentExpansionTime < totalTime)
        m_ContractionRoutine = StartCoroutine(ExpandImages(totalTime));

    else if (m_ContractionRoutine != null)
    {
        StopCoroutine(m_ExpansionRoutine);
        InvokeExpansionComplete();

        Debug.Log("Expansion Complete: " + totalTime / m_CurrentExpansionTime);
        m_CurrentExpansionTime = 0;
    }
}

好吧,第一个明显的区别是,在 ContractImages 中,你从当前位置开始,它越来越接近目的地,而在 ExpandImages 中,你从一个恒定的位置开始.所以当然 ContractImages lerp 会在 lerp 中间进行得更快。

例如,假设展开位置为new Vector3(1f,0f,0f)totalTime1fTime.deltaTime0.1f:

第一帧

合同

r.localPosition = Vector3.Lerp(new Vector3(1f,0f,0f), Vector3.zero, 0.1f); 
             // = new Vector3(0.9f, 0f, 0f); 
             // dist = 0.1f

展开

r.localPosition = Vector3.Lerp(Vector3.zero, new Vector3(1f,0f,0f), 0.1f);
             // = new Vector3(0.1f, 0f, 0f);
             // dist = 0.1f

第二帧

合同

r.localPosition = Vector3.Lerp(new Vector3(0.9f,0f,0f), Vector3.zero, 0.2f); 
             // = new Vector3(0.78f, 0f, 0f);
             // dist = 0.22f

展开

r.localPosition = Vector3.Lerp(Vector3.zero, new Vector3(1f,0f,0f), 0.2f);
             // = new Vector3(0.2f, 0f, 0f);
             // dist = 0.2f

第三帧

合同

r.localPosition = Vector3.Lerp(new Vector3(0.78f,0f,0f), Vector3.zero, 0.3f); 
             // = new Vector3(0.546f, 0f, 0f);
             // dist = 0.454f

展开

r.localPosition = Vector3.Lerp(Vector3.zero, new Vector3(1f,0f,0f), 0.3f);
             // = new Vector3(0.3f, 0f, 0f);
             // dist = 0.3f

基本上,在这两种情况下,您都应该考虑常量之间的 lerping:

IEnumerator ContractImages(float totalTime)
{
    yield return m_WaitForEndOfFrame;

    m_CurrentContractionTime += Time.deltaTime;

    float t =  m_CurrentContractionTime / totalTime;

    Debug.Log(string.Format("Expansion Rate Factor: {0}", t));
    Debug.Log(string.Format("Current Expansion Time: {0}", m_CurrentContractionTime));

    for (int i = 0; i < m_MovingImages.Length; i++)
    {
        var r = m_MovingImages[i].GetComponent<RectTransform>(); //Optimize

        r.localPosition = Vector3.Lerp(m_StartingVectors[i], Vector3.zero, t);
    }

    if (m_CurrentContractionTime < totalTime)
        m_ContractionRoutine = StartCoroutine(ContractImages(totalTime));

    else if (m_ContractionRoutine != null)
    {
        StopCoroutine(m_ContractionRoutine);
        InvokeContractionComplete();
        m_CurrentContractionTime = 0;
    }
}