Increasing an GamObjects transform.localScale by 1 with OnTriggerStay

alt text

I want a trigger to increase its size once a collider "collides" with it, the problem im having is that the whole time the collider is "colliding" with the trigger the size increases. I want it to increase by 1 unit, then if another collider makes contact increase by a further 1 unit etc.

var xzlen = 1;

function OnTriggerStay(other : Collider){

    var myTrig = GameObject.FindWithTag("myTrigger");
    myTrig.transform.localScale = Vector3(xzlen,1,xzlen);

    if(other.CompareTag("myCollider")){
        xzlen++;
    }   
}

Use OnTriggerEnter instead of Stay.

Enter is called once as a collision begins, Stay is called while the objects are colliding and Exit is called once when the object stops colliding.

function OnTriggerEnter(other : Collider)
{
    if (other.CompareTag("myCollider"))
    {
       transform.localScale += Vector3(1, 0, 1);
    }
}

That if statement is only necessary if you are expecting it to collide with something else that you don't want to trigger a size increase. If not you can strip the if out.

This script will increase the scale of your trigger in X and Z axis by 1 for each instance of a collider that collides with it (if the collider is tagged "myCollider").

To Lerp from the current scale to your target scale, you can use the following script. Send however many seconds you'd like the interpolation to take as the parameter.

function OnTriggerEnter(other : Collider)
{
   // I've left out checking for the collider, if you need it add it.

   StartCoroutine(LerpScale(2.0f));
}

function LerpScale(time : float)
{
   var originalScale = transform.localScale;
   var targetScale = originalScale + Vector3(1.0f, 0.0f, 1.0f);
   var originalTime = time;

   while (time > 0.0f)
   {
      time -= Time.deltaTime;

      transform.localScale = Vector3.Lerp(targetScale, originalScale, time / originalTime);

      yield;
   }
}

That should do it. The divide in the Lerp is necessary to have times longer than 1 second. I hope I have all the JavaScript stuff correct.

I know it is already answered; Alternatively, you can set integer variable change the Vector3 values instead of creating new Vector3 to add up.
This worked out for me;
I have no idea the performance difference between those. Again, just sharing an alternative;

    //declaring variables...
    private int xValue;
    private int zValue;

    //assigning initial values to each value or collect from the gameobject...
    xValue = transform.localScale.x;
    zValue = transform.localScale.z;
    
    function OnTriggerEnter(other : Collider)
    {
        if (other.CompareTag("myCollider"))
        {
            xValue ++
            zValue ++
            transform.localScale = Vector3(xValue, 0, zValue);
        }
    }

Hi, i’m searching for another concept.

I wan t to scale down to 0% items that are out of a certain volume (Sphere).

Is their a way to do this ? @Caiuse @KeithK @Zennig