Hello everyone,
I’ve made a little project that increases the player’s scale by one on each axis when the player presses space. I’m trying to figure out how to smoothly lerp between the old size of the player and the new size of the player. Currently, the change happens instantly and the player just snaps to its new size each time space is pressed. This was my initial code (attached to the player object):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScaleLerper : MonoBehaviour
{
Vector3 originalSize = Vector3.one;
Vector3 currentSize;
public float multiplier;
public float smoothTime;
void Start() {
if (currentSize == Vector3.zero) {
currentSize = originalSize;
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("space")) {
currentSize += new Vector3(multiplier, multiplier, multiplier);
transform.localScale = currentSize;
}
}
}
and then I tried to use Vector3.Lerp, but it didn’t quite work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScaleLerper : MonoBehaviour
{
Vector3 originalSize = Vector3.one;
Vector3 currentSize;
public float multiplier;
public float smoothTime;
void Start() {
if (currentSize == Vector3.zero) {
currentSize = originalSize;
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("space")) {
currentSize += new Vector3(multiplier, multiplier, multiplier);
transform.localScale = Vector3.Lerp(transform.localScale, currentSize, smoothTime * Time.deltaTime);
}
}
}
Instead of smoothly transitioning between the localScale values, the player’s size still changed rigidly. Does anyone have any ideas?