为什么我的图像位置以两种不同的速度旋转



这里有一些简单的代码。我遇到的问题是,当图像离开中心时,图像收缩到中心(局部位置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中,你是从一个不变的位置开始。所以ContractImageslerp当然会在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;
}
}

最新更新