Resizing an object when zoomed out

There are two objects. When colliding with one, the player decreases, and with the other, it increases. I wrote a script, but it only works as long as it contains values greater than 0. How can I return the object to its original size (0,0,0)?

 private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Big")
        {
            transform.localScale += new Vector3(1, 1, 0);
            Debug.Log("Big");
        }

 
        if (other.tag == "Small")
        {
            transform.localScale -= new Vector3(0, 0, 0);
            Debug.Log("Small");
        }

8742975--1184148--gif1.gif

The issue you have, is that Vector3(0,0,0) is 0 size. Therefore when you are trying to subtract from your localScale you are basically saying that you are wanting to reduce the localScale by 0, which won’t change it.

Why not just define in your class a private Vector3 that holds the original transform.localScale value, and then when you need it set the transform.localScale to be the value in that variable?

Something a bit like this (I haven’t tested this):

If you put this in your class variable definitions:
Vector3 originalLocalScale;
In Start (or Awake):
originalLocalScale = transform.localScale;
and then in your OnTriggerEnter2D method, change this line transform.localScale -= new Vector3(0, 0, 0); to be this: transform.localScale = originalLocalScale;

Not tested this, so I may have missed something - but that would be the way I would go about it.
You could even make it so that you have variables defined for the Big Scale and Small Scale and then just use those in your trigger method rather than doing addition/subtraction to your transform.localScale.

2 Likes

Thanks helped, but now the resizing is in the air. Is there any way to make it resize relative to Y?

8743236--1184193--ds.gif

Problem solved. It was necessary to put the Pivot at the bottom of the sprite.