Scale GameObject over time

This can be done with the Time.deltaTime and Vector3.Lerp in a coroutine function. Similar to Rotate GameObject over time and Move GameObject over time questions. Modified it a little bit to do just this.

bool isScaling = false;

IEnumerator scaleOverTime(Transform objectToScale, Vector3 toScale, float duration)
{
    //Make sure there is only one instance of this function running
    if (isScaling)
    {
        yield break; ///exit if this is still running
    }
    isScaling = true;

    float counter = 0;

    //Get the current scale of the object to be moved
    Vector3 startScaleSize = objectToScale.localScale;

    while (counter < duration)
    {
        counter += Time.deltaTime;
        objectToScale.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
        yield return null;
    }

    isScaling = false;
}

USAGE:

Will scale GameObject within 20 seconds:

StartCoroutine(scaleOverTime(cylinder.transform, new Vector3(0, 0, 90), 20f));

Leave a Comment