Scaling overtime

So I got the rescale script, but i’m not quite sure how to make it rescale over time. Like over the timespan of a minuet, it slowly grows the X coordinate to let’s say 10, so it goes from 1 or whatever and overtime grows to 10. Script is:

  bool resized = false;
    void Update()
    {
        if (Input.GetKey("f") && resized == false)  // If the player is pressing the "f" key
        {
            Vector3 CurrentScale = transform.localScale;
            Vector3 newScale = CurrentScale + new Vector3(0, 0, -0.4f);
            transform.localScale = newScale;
            resized = true;

Coroutines are best for this. Here’s an example:

if (..) {
StartCoroutine(ScaleOverTime(transform.localScale, transform.localScale + new Vector3(0f,0f,-0.4f), 60f));
}

.....

private IEnumerator ScaleOverTime(Vector3 startScale, Vector3 endScale, float duration) {
for (float t = 0f; t < duration; t += Time.deltaTime) {
float lerpValue = t / duration;
transform.localScale = Vector3.Lerp(startScale, endScale, lerpValue);
yield return 0;
}
transform.localScale = endScale;
}
2 Likes