OnCollisionEnter transform.localScale decreasing and increasing scale of gameObject

Hello everyone,

I am trying to have game objects decrease in scale, up to when they are triggered by a specific collider on which they should be increasing in scale (and decreasing again from that point upon trigger exit).

As of now the objects decrease, but do not seem to react on the ‘OnCollisionEnter’
I wonder whether I need a line of code to tell the original decreasing scale to stop once it is triggered, before it can increase in scale.

Any help is very much appreciated!

Code below:

using UnityEngine;
public class ScaleDecIn : MonoBehaviour
{

    void Update()
    {
        // Widen the object by x, y, and z values
        transform.localScale += new Vector3(-0.00001f, -0.00001f, -0.00001f);
    }

    void OnCollisionEnter(Collider other)
    {
        if (other.CompareTag("Rain"))
    {
            Vector3 add = new Vector3(0f, 0f, 0f);
            transform.localScale += new Vector3(0.001f, 0.001f, 0.001f);
    }
    }
}

Thank you!

try with this :slight_smile:

    void OnCollisionEnter(Collision collision)
    {
        if(collision.collider.CompareTag("Rain"))
        {
            Vector3 add = new Vector3(0f, 0f, 0f);
            transform.localScale += new Vector3(0.001f, 0.001f, 0.001f);           
        }
    }

Hey :slight_smile: Thank you so much for taking the time to help!
Unfortunately it didn’t work. I also tried having just this part of the code, without the default decreasing scale part, because I thought it might be overriding it, but also to no avail.
I would appreciate any other idea you might have for me to try out.

Thanks!

you actually want to decrease it’s scale when exit from the collision and increase it when it’s collide? i’m right?

there is a problem here because you are updating it’s scale every frame, do you need it?

what are you doing here is just decrease it’s scale every frame, but in the same frame that collide there is no change…

Yes exactly! I want to mimic growth, so for that reason I would need it to update its scale every frame or every other frame.

Can you reiterate on the last part of your response? I’m not sure I understand. But yes, so far it is decreasing every frame, but nothing happens upon collide.

Thank you!

i’m not using Collider so i’m not sure about this but you can try with this

    private bool pauseDecrease;



    void Update()
    {
        if(!pauseDecrease)
        {
            // Widen the object by x, y, and z values
            transform.localScale += new Vector3(-0.00001f, -0.00001f, -0.00001f);          
        }
    }

    void OncollisionEnter(Collision col)
    {
        if(col.collider.CompareTag("Rain"))
        {
            pauseDecrease = true;
            Vector3 add = new Vector3(0f, 0f, 0f);
            transform.localScale += new Vector3(0.001f, 0.001f, 0.001f);
        }
    }

    void OnCollisionExit(Collision col)
    {
        if(col.collider.CompareTag("Rain"))
        {
            pauseDecrease = false;
        }      
    }

Just started learning Unity and the first thing I tried was scaling an object on collision too.

What I know is the OnCollisionEnter event just takes 1 frame to calculate, then the other collider bounces back. It may sometimes mimick longer contact but under the hood its all done within one frame. So you would jus get a one time increase of 0.00001f when things collide.

If you want it to grow over time, during a couple of frames you could use a Vector3.Lerp(). This will increase your scale in a nice fashion from a ‘initial scale’ to a ‘desired scale’ over several frames. Also works with position and rotation.