1

I'm trying to scale an object gradually along the Y axis until it's the same size as another gameobject, or I could enter the dimensions manually. Right now I have this.

growScale = ObjectToScale.transform.localScale;
targetSize = TargetObject.GetComponent<RectTransform>().localScale;
isScale = true;

void Update()
{
if (isScaleCurtainBottomEasyRead)
    {
        growScale += new Vector3(0, 1, 0) * 0.01f;
        ObjectToScale.transform.localScale = Vector3.Lerp(ObjectToScale.transform.localScale, growScale, 0.5f);
        if (ObjectToScale.transform.localScale == targetSize)
            {
                isScaleCurtainBottomEasyRead = false;
            }

    }
}

The object never stops scaling. I assume that's because it never gets to the exact same size and is slightly over or under, but I can't figure out how to stop it from scaling. I'm willing to enter the dimensions manually instead of using TargetObject as a reference for the size it should be, but everything I can find online talks about scaling indefinitely or using a bool to stop it rather than when it reaches a certain size. How can I solve this?

Edit: Programmer marked this as a duplicate, but the linked question asks how to scale the object for a certain period of time. I need to scale it until it reaches a specific size, not for a certain number of seconds.

NomadicBinary
  • 67
  • 1
  • 2
  • 8
  • 1
    There's no guarantee that they'll ever be exactly equal. So, check for when its `y` component is greater or equal: `if (ObjectToScale.transform.localScale.y >= targetSize.y)` – Ruzihm Nov 21 '18 at 18:15
  • 1
    Also why not just use `ObjectToScale.transform.localScale = Vector3.Scale(ObjectToScale.transform.localScale, new Vector3(1f,1.01f,1f));`? Lerp is inappropriate here if you're using a constant `0.5f` and you don't have a reason to go exactly 50% between two values. Even better, use `Vector3.Scale(ObjectToScale.transform.localScale, new Vector3(1f,Mathf.Pow(1.01f,Time.deltaTime*60f),1f));` so that it doesn't slow down on framerate drops. – Ruzihm Nov 21 '18 at 18:25
  • 1
    If you want to make really sure you don't exceed the y scale, you can also use `Mathf.Max`. You'll have to figure out each component of the new scale separately though: `ObjectToScale.transform.localscale = new Vector3(ObjectToScale.transform.localScale.x, Mathf.Max(ObjectToScale.transform.localScale.y *Mathf.Pow(1.01f,Time.deltaTime*60f),targetSize.y, ObjectToScale.transform.localScale.z);` – Ruzihm Nov 21 '18 at 18:40
  • @Ruzihm Thank you so much for the help with the scaling code. I still can't seem to get it to stop scaling even with your if statement, but at least i know I'm missing something and have the code to start troubleshooting it. – NomadicBinary Nov 21 '18 at 18:50
  • 1
    I think I figured out the problem, I changed the height of the target object, not the local scale in Unity. If I change the size with the local scale I think it'll work. Thank you so much again. – NomadicBinary Nov 21 '18 at 18:54
  • Edit: I asked another question but I'll just start a new question separately as this question has been answered. Thanks again. – NomadicBinary Nov 21 '18 at 18:59
  • 1
    Be sure to include some screenshots of the hierarchy you're working with. – Ruzihm Nov 21 '18 at 19:02

0 Answers0