- Home /
Scale with coroutine and lerp
Hi guys, my code is not working, surely a dumb mistake but i can't found it. The scripts scale bigger a game object and then return to his sclae, using Vector3.lerp. The first part works good : Fade in (I know it's not a correct name for this function) and for the FadeOut i've just invert the script but i think i'm missing something. if anyone can tell me what's the problem, here's the code :
IEnumerator FadeIn(GameObject obj)
{
float progress = 0;
while (progress <= 1)
{
obj.transform.localScale = Vector3.Lerp(startPos, endPos, progress);
progress += Time.deltaTime;
yield return null;
}
StartCoroutine(FadeOut(obj));
}
IEnumerator FadeOut(GameObject obj)
{
float progress = 1;
while (progress >= 0)
{
obj.transform.localScale = Vector3.Lerp(obj.transform.localScale, startPos, progress);
progress -= Time.deltaTime;
yield return null;
}
}
Answer by Hellium · Jul 19, 2020 at 11:30 AM
IEnumerator ScaleUpAndDown(Transform transform, Vector3 upScale, float duration)
{
Vector3 initialScale = transform.localScale;
for(float time = 0 ; time < duration * 2 ; time += Time.deltaTime)
{
float progress = Mathf.PingPong(time, duration) / duration;
transform.localScale = Vector3.Lerp(initialScale, upScale, progress);
yield return null;
}
transform.localScale = initialScale;
}
// ...
StartCoroutine(ScaleUpAndDown(otherObject.transform, new Vector3(2f, 2f, 2f), 1f);
Your answer
Follow this Question
Related Questions
How can I stop a parented object from resetting its transform when a parent animation play? 1 Answer
Scale animation with model 1 Answer
Why does my animation warp when I adjust the scale in Unity? 1 Answer
How should I scale an object with collider? 1 Answer
Changing object scale in script while animation is running 1 Answer