Lerping multiple gameobjects precisely
For a project I need to continously lerp bars from left to right.
Each bar has a script on it, that lerps it to the right. When the bar hits the right border, it gets set to left border and start again to lerp from left to the right. The lerping works fine.
void FixedUpdate()
{
if (BarSetting.Instance.Running)
{
//bar has reached the next position
if (Vector3.Distance(transform.position, nextPosition) <= 0)
{
//when the bar has reached set the next position
startPosition = movementType.getFirst();
transform.position = startPosition;
nextPosition = movementType.getLast();
//reset the timer for the lerp transition
float distanceBetweenStartAndNext = Vector3.Distance(startPosition, nextPosition);
timeToReachNextPosition = Utilities.CircumferenceInUnits(BarSetting.Instance.Speed) * distanceBetweenStartAndNext;
timePassed = 0;
}
else
{
//move the bar closer to the next position
timePassed += Time.deltaTime / timeToReachNextPosition;
this.transform.position = Vector3.Lerp(startPosition, nextPosition, timePassed);
}
}
}
The problem is that the space between the bars doesn't stay exact the same. With just a few bars it works fine, but I need to lerp like 30 bars and there the space varies a lot.
I dont think, that the problem lies with the lerping itself. It seems, that Unity has problems with handling multiple lerping exactly.
Startposition of all bars:
How they end up after a few rounds
Has somebody an idea for a fix or an alternative solution?
Thanks in advance.
If everyone is interested in my current solution: The problem with my scene was, that i lerped each bar individual, which isnt very precise. So I've parented half the bars on one empty gameobejct and the other half on another empty gameobject. The script, which was lerping the bars, got modified to lerp the gameobjects parenting the bars. With this modification the bars keep exact the same space inbetween.