- Home /
Vector3 Lerp mis-match
I'm instantiating a Gameobject with a number of children at a certain offset. This Co-Routine is called once per child. I want to randomize the children's Y position and Lerp them back to their correct endPosition. However, for some reason my endPosition is getting affected by this change. The parts end up assembling at whatever the randomized Yoffset of the start position was.
I'm only randomizing the startPos.y; why is my endPos being affected?
A number of people have been stumped by this problem, so any help would be great.
IEnumerator TransitionAnim(Transform part, bool start)
{
float t = 0;
if (start)
{
Vector3 startPos = new Vector3(part.position.x, Random.Range(30,-30), part.position.z);
Vector3 endPos = new Vector3(part.position.x - startingOffset, part.position.y, part.position.z);
while (t < duration)
{
t += Time.deltaTime;
part.position = Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0,1F,t/duration));
yield return 0;
}
part.position = endPos;
}
}
Comment