Why when scaling object it's changing the object scaling shape when changing the speed value ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Walls : MonoBehaviour
{
    public GameObject gameObjectToRaise;
    public int raiseAmount;
    public float speed;

    private GameObject objtoraise;

    private void Start()
    {
        objtoraise = Instantiate(gameObjectToRaise);
        objtoraise.name = "Walls";
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            objtoraise.transform.localScale = Vector3.Lerp(objtoraise.transform.localScale,
                new Vector3(objtoraise.transform.localScale.x + raiseAmount,
                objtoraise.transform.localScale.y + raiseAmount,
                objtoraise.transform.localScale.z), speed * Time.deltaTime);
        }
    }
}

If I set the speed to 1 in the editor it will scale to some shape but if I set the speed to 0.3 or to 5 it will show a different shape. The speed for some reason have a factor on the scaling.

Instead of that atrociously gnarly line 24 which sprawls across four lines of text, I suggest you instead calculate the individual inputs into temporary variables first so that you can reason about each step of the process, printing the values out with Debug.Log()

For instance, the first thing that springs to mind is that your localScale is being manipulated, AND you are subsequently feeding it back into the first term of the Vector3.Lerp(). That seems … wrong. Wouldn’t you want to freeze the initial location if you were doing a Lerp? Or are you going for a damping effect?

1 Like

yes I want to freeze the location. the idea is to make smooth scaling that is why I’m using lerp in this case.

This is working nice.

private void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            StartCoroutine(ScaleOverSeconds(objtoraise, new Vector3(raiseAmount, raiseAmount,
                objtoraise.transform.localScale.z), 5f));
        }
    }

    public IEnumerator ScaleOverSeconds(GameObject objectToScale, Vector3 scaleTo, float seconds)
    {
        float elapsedTime = 0;
        Vector3 startingScale = objectToScale.transform.localScale;
        while (elapsedTime < seconds)
        {
            objectToScale.transform.localScale = Vector3.Lerp(startingScale, scaleTo, (elapsedTime / seconds));
            elapsedTime += Time.deltaTime;
            yield return new WaitForEndOfFrame();
        }

        objectToScale.transform.localScale = scaleTo;
    }
1 Like