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);
}
}
}
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.
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.
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.