Search code examples
unity-game-enginescalelerp

unity lerp scale doesn't work


I am trying to make an object scale from zero to it's normal size when I instantiate it, so it will look like it popped to the screen.

So when the object start I get it's normal size then update it to zero, and then in update I am scaling it.

This is my code:

void Start()
{
    normalScale = transform.localScale;
    transform.localScale *= 0.1f;
}
void Update()
{
    transform.localScale = Vector3.Lerp(transform.localScale * 0.1f, transform.localScale, 5f * Time.deltaTime);
    // destroy item
    if (transform.localScale == normalScale)
    {
        transform.localScale = transform.localScale * 0.1f;
    }
}

Thank you.


Solution

  • With this you're always changing from it's current scale, which of course you changed last update

    transform.localScale = Vector3.Lerp(transform.localScale * 0.1f, transform.localScale, 5f * Time.deltaTime);
    

    What you need to do is create two Vector3 outside of the update function, one for the start size, one for the final size

    Vector3 start = Vector3.zero;
    Vector3 end = new Vector3(1,1,1);
    

    You'll also need a timer:

    float lerpTime = 0;

    Altogether you get

    transform.localScale = Vector3.Lerp(start, end, lerpTime);
    lerpTime += Time.deltaTime // times whatever multiplier you want for the speed