- Home /
Why is my lerp not working?
I'm trying to lerp a UI element between two locations. I also tried SmoothDamp.
public void Clicked()
{
RectTransform rect = GetComponent<RectTransform>();
if (opened)
{
start = transform.localPosition;
while(currentTime <= smoothTime)
{
currentTime += Time.deltaTime;
normalizedValue = currentTime / smoothTime;
Debug.Log(normalizedValue + "opened");
//rect.localPosition = Vector3.SmoothDamp(start, pos1, ref velocity, smoothSpeed);
rect.anchoredPosition = Vector3.Lerp(start, pos1, normalizedValue);
}
currentTime = 0;
opened = false;
}
else
{
start = transform.localPosition;
while(currentTime <= smoothTime)
{
currentTime += Time.deltaTime;
normalizedValue = currentTime / smoothTime;
Debug.Log(normalizedValue + "closed");
//rect.localPosition = Vector3.SmoothDamp(start, pos2, ref velocity, smoothSpeed);
rect.anchoredPosition = Vector3.Lerp(start, pos2, normalizedValue);
}
currentTime = 0;
opened = true;
}
}
Answer by Bonfire-Boy · Feb 12, 2020 at 04:43 PM
That's going to do the whole movement in one go. You need to start by converting it to a coroutine and yielding after each incremental move.
If you don't know what I mean by that, I would suggest searching this site for something like "moving using coroutine"
I'm not offended, but curious about the downvote. $$anonymous$$y answer is fairly terse I admit, but only because it's a subject that's been covered at length on this site already. The search I suggested will lead one quickly to several answers with sample code that will help.
If someone has an issue with the essence of what I'm saying - that the problem is that the code doesn't yield between the incremental movements and so they all happen in one frame, then please say what the perceived issue is and perhaps together we can help the OP.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to smooth out movement in a 3D space? 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer
Movement Script 1 Answer