- Home /
object's children not moving with it when using a Lerp-ing coroutine
The title basically says what my problem is, but I'll give more detail here: I'm working on a building block application of sorts and when the user disconnect previously-connected pieces, the pieces that the selected piece is disconnecting from should explode away from the central piece. However, if I do this translation with a coroutine that animates the movement over a short period of time, only the calling transform will move, not its children. But here's the kicker: if I do this translation instantly without a coroutine, with a coroutine that instantly translates and then returns, or with a coroutine that translates it with the Lerp function over a period of time and then returns after the translation, it works beautifully. Any ideas why this might be happening? I've been racking my brain all day on this problem. Here's the LerpToPosition coroutine that's causing the issue (it's triggered on a button press):
private IEnumerator LerpToPosition_(Vector3 newPosition)
{
float t = 0.0f;
Vector3 startingPos = transform.position;
// if the object is close to the new location, make the transition quicker
float lerpSpeed =
Mathf.Min(posLerpCoefficient * (transform.position - newPosition).magnitude, maxLerpDuration);
while (t < 1.0f) {
t += Time.deltaTime * (Time.timeScale / lerpSpeed);
transform.position = Vecto3.Lerp(startingPos, newPosition, t);
yield return 0;
}
}
For the sake of completeness, here's a version of that coroutine that does what I want it to (minus, of course, the animation):
private IEnumerator LerpToPosition_(Vector3 newPosition)
{
float t = 0.0f;
Vector3 startingPos = transform.position;
// if the object is close to the new location, make the transition quicker
float lerpSpeed =
Mathf.Min(posLerpCoefficient * (transform.position - newPosition).magnitude, maxLerpDuration);
while (t < 1.0f) {
t += Time.deltaTime * (Time.timeScale / lerpSpeed);
transform.position = Vecto3.Lerp(startingPos, newPosition, t);
}
yield return 0;
}
And another version that works:
private IEnumerator LerpToPosition_(Vector3 newPosition)
{
float t = 0.0f;
Vector3 startingPos = transform.position;
// if the object is close to the new location, make the transition quicker
float lerpSpeed =
Mathf.Min(posLerpCoefficient * (transform.position - newPosition).magnitude, maxLerpDuration);
transform.position = newPosition;
yield return 0;
}
What the hell is going on in my code? And how do I fix it? If you'd like I can include screenshots of what exactly is happening.
First and second code posts are identical.
It's also quite hard to grasp what you mean, because you're talking about parents, children and animation, but kind of only have one script affecting one position.
try adding a while(true) in it , not sure if that will do a change though