Scale up on Collision?

Hi all!
I’m trying to Scale up my Player when he walk into a BoxCollider. (On Trigger)
With this Script attached to the Box-Collider-Object, it scale up the Box-Collider - not the player:

void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Player")
        {
            // Widen the object by 0.1
            transform.localScale += new Vector3(2f, 2f, 2f);
        }
           
    }

So I want to opposite to happen :confused:
When I Attached this line to my Player Script, its basically working.

void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "ScaleUp")
        {
            // Widen the object by 0.1
            transform.localScale += new Vector3(2f, 2f, 2f);
        }           
    }

But I want to attach the Script to the BoxCollider-Object so I can change the scale from the Inspector instead.
Any idea how to make that work?

Thanks in advance!

ontriggerenter gives you the colliding object:
void OnTriggerEnter2D(Collider2D other)

so it should work with,
other.transform.localScale…

1 Like

So, just transform.localScale is referencing to GameObject which has this script, if your have this script on box, Collider2D other is your player, so if you would like to scale player, get transform of other and scale it.

1 Like

Thanks, That worked!
But when I exit the box-collider he remain the new “Big size”… :confused:
Any idea how to scale him back to normal size after exiting?

I tried this;

void OnTriggerExit2D(Collider2D other)
    {
        if (other.tag == "Player")
        {
            other.transform.localScale += new Vector3(0.81, 0.81, 0.81);
        }
    }

0.81 is what the Player Objects Scale-value in the Inspector… I just assumed I shuld use this :confused:

you have += there,
maybe you just want =

1 Like

Wow! That worked.
Amazing that 1 little “+” mark can mess it up :stuck_out_tongue:
Thanks alot!