Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

OnCollisionEnter transform.localScale decreasing and increasing scale of gameObject

Discussion in 'Scripting' started by SinaBanana1, Nov 7, 2019.

  1. SinaBanana1

    SinaBanana1

    Joined:
    Oct 20, 2019
    Posts:
    8
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. public class ScaleDecIn : MonoBehaviour
    3. {
    4.  
    5.     void Update()
    6.     {
    7.         // Widen the object by x, y, and z values
    8.         transform.localScale += new Vector3(-0.00001f, -0.00001f, -0.00001f);
    9.     }
    10.  
    11.     void OnCollisionEnter(Collider other)
    12.     {
    13.         if (other.CompareTag("Rain"))
    14.     {
    15.             Vector3 add = new Vector3(0f, 0f, 0f);
    16.             transform.localScale += new Vector3(0.001f, 0.001f, 0.001f);
    17.     }
    18.     }
    19. }
    Thank you!
     
  2. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126

    try with this :)

    Code (CSharp):
    1.     void OnCollisionEnter(Collision collision)
    2.     {
    3.         if(collision.collider.CompareTag("Rain"))
    4.         {
    5.             Vector3 add = new Vector3(0f, 0f, 0f);
    6.             transform.localScale += new Vector3(0.001f, 0.001f, 0.001f);          
    7.         }
    8.     }
     
  3. SinaBanana1

    SinaBanana1

    Joined:
    Oct 20, 2019
    Posts:
    8
    Hey :) 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!
     
  4. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126
    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..
     
  5. SinaBanana1

    SinaBanana1

    Joined:
    Oct 20, 2019
    Posts:
    8
    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!
     
  6. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126
    i'm not using Collider so i'm not sure about this but you can try with this

    Code (CSharp):
    1.     private bool pauseDecrease;
    2.  
    3.  
    4.  
    5.     void Update()
    6.     {
    7.         if(!pauseDecrease)
    8.         {
    9.             // Widen the object by x, y, and z values
    10.             transform.localScale += new Vector3(-0.00001f, -0.00001f, -0.00001f);          
    11.         }
    12.     }
    13.  
    14.     void OncollisionEnter(Collision col)
    15.     {
    16.         if(col.collider.CompareTag("Rain"))
    17.         {
    18.             pauseDecrease = true;
    19.             Vector3 add = new Vector3(0f, 0f, 0f);
    20.             transform.localScale += new Vector3(0.001f, 0.001f, 0.001f);
    21.         }
    22.     }
    23.  
    24.     void OnCollisionExit(Collision col)
    25.     {
    26.         if(col.collider.CompareTag("Rain"))
    27.         {
    28.             pauseDecrease = false;
    29.         }      
    30.     }
     
  7. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    56
    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.