Increasing and decreasing scale upon entering Collision

Hello!

Ideally I would want my game object to continually decrease in size and increase for the time it stays within a collider.
So far the game object will increase in size, but not decrease upon exiting. Instead it will just stop at whatever size it reached when it was inside the collider. It will increase continually in size once it re-enters the collider.
When I have the decreasing part in the void update the game object will only decrease, but not increase upon entering the collider.
I’d be grateful for any hints. Thank you!

My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AmandaSinaTry : MonoBehaviour
{
    public bool enter = true;
    public bool stay = true;
    public bool exit = true;
    //public float moveSpeed;

    void Awake()
    {
        // move and reduce the size of the cube
        //transform.position = new Vector3(0, 0.25f, 0.75f);
        transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);

        // add isTrigger
        var boxCollider = gameObject.AddComponent<BoxCollider>();
        boxCollider.isTrigger = true;

        //moveSpeed = 1.0f;

        // create a sphere for this cube to interact with
        GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphere.gameObject.transform.position = new Vector3(0, 0, 0);
        sphere.gameObject.AddComponent<Rigidbody>();
        sphere.gameObject.GetComponent<Rigidbody>().isKinematic = true;
        sphere.gameObject.GetComponent<Rigidbody>().useGravity = true;

    }

    void Update()
    {
        //transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis("Vertical") * moveSpeed);
        //transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);


    }

    private void OnTriggerEnter(Collider other)
    {
        if (enter)
        {
            Debug.Log("entered");

        }
    }

    // stayCount allows the OnTriggerStay to be displayed less often
    // than it actually occurs.
    private float stayCount = 0.0f;
    private void OnTriggerStay(Collider other)
    {
        if (stay)
        {
            if (stayCount > 0.25f)
            {
                Debug.Log("staying");
                stayCount = stayCount - 0.25f;
                Vector3 add = new Vector3(0.1f, 0.1f, 0.1f);
                transform.localScale += new Vector3(0.001f, 0.001f, 0.001f);
            }
            else
            {
                stayCount = stayCount + Time.deltaTime;
            }
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (exit)
        {
            Debug.Log("exit");
            if (transform.localScale.x > 0 && transform.localScale.y > 0 && transform.localScale.z > 0)
            {
                Vector3 add = new Vector3(0f, 0f, 0f);
                transform.localScale += new Vector3(-0.0001f, -0.0001f, -0.0001f);
            }

            if (transform.localScale.x < 0.1 && transform.localScale.y < 0.1 && transform.localScale.z < 0.1)
            {
                transform.localScale = Vector3.zero;
            }

        }
    }
}

Just have a bool which you set to true in OnTriggerEnter, and you set to false in OnTriggerExit. Move all the size changing code from the OnTrigger methods to Update. In Update if the bool is true you increase scale, if false you decrease scale.

private bool insideTrigger = false;

void Update()
{
    if (insideTrigger)
    {
        //increase scale code here
    }
    else
    {
        //decrease scale code here
    }
}


void OnTriggerEnter(Collider other)
{
    insideTrigger = true;
}

void OnTriggerExit(Collider other)
{
    insideTrigger = false;
}
2 Likes

Hello we love you!!!
You just helped something we were working on for two days- thank you so much.

2 Likes