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

Increasing and decreasing scale upon entering Collision

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

  1. SinaBanana1

    SinaBanana1

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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AmandaSinaTry : MonoBehaviour
    6. {
    7.     public bool enter = true;
    8.     public bool stay = true;
    9.     public bool exit = true;
    10.     //public float moveSpeed;
    11.  
    12.     void Awake()
    13.     {
    14.         // move and reduce the size of the cube
    15.         //transform.position = new Vector3(0, 0.25f, 0.75f);
    16.         transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    17.  
    18.         // add isTrigger
    19.         var boxCollider = gameObject.AddComponent<BoxCollider>();
    20.         boxCollider.isTrigger = true;
    21.  
    22.         //moveSpeed = 1.0f;
    23.  
    24.         // create a sphere for this cube to interact with
    25.         GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    26.         sphere.gameObject.transform.position = new Vector3(0, 0, 0);
    27.         sphere.gameObject.AddComponent<Rigidbody>();
    28.         sphere.gameObject.GetComponent<Rigidbody>().isKinematic = true;
    29.         sphere.gameObject.GetComponent<Rigidbody>().useGravity = true;
    30.  
    31.     }
    32.  
    33.     void Update()
    34.     {
    35.         //transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis("Vertical") * moveSpeed);
    36.         //transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
    37.  
    38.  
    39.     }
    40.  
    41.     private void OnTriggerEnter(Collider other)
    42.     {
    43.         if (enter)
    44.         {
    45.             Debug.Log("entered");
    46.  
    47.         }
    48.     }
    49.  
    50.     // stayCount allows the OnTriggerStay to be displayed less often
    51.     // than it actually occurs.
    52.     private float stayCount = 0.0f;
    53.     private void OnTriggerStay(Collider other)
    54.     {
    55.         if (stay)
    56.         {
    57.             if (stayCount > 0.25f)
    58.             {
    59.                 Debug.Log("staying");
    60.                 stayCount = stayCount - 0.25f;
    61.                 Vector3 add = new Vector3(0.1f, 0.1f, 0.1f);
    62.                 transform.localScale += new Vector3(0.001f, 0.001f, 0.001f);
    63.             }
    64.             else
    65.             {
    66.                 stayCount = stayCount + Time.deltaTime;
    67.             }
    68.         }
    69.     }
    70.  
    71.     private void OnTriggerExit(Collider other)
    72.     {
    73.         if (exit)
    74.         {
    75.             Debug.Log("exit");
    76.             if (transform.localScale.x > 0 && transform.localScale.y > 0 && transform.localScale.z > 0)
    77.             {
    78.                 Vector3 add = new Vector3(0f, 0f, 0f);
    79.                 transform.localScale += new Vector3(-0.0001f, -0.0001f, -0.0001f);
    80.             }
    81.  
    82.             if (transform.localScale.x < 0.1 && transform.localScale.y < 0.1 && transform.localScale.z < 0.1)
    83.             {
    84.                 transform.localScale = Vector3.zero;
    85.             }
    86.  
    87.         }
    88.     }
    89. }
    90.  
    91.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.

    Code (csharp):
    1. private bool insideTrigger = false;
    2.  
    3. void Update()
    4. {
    5.     if (insideTrigger)
    6.     {
    7.         //increase scale code here
    8.     }
    9.     else
    10.     {
    11.         //decrease scale code here
    12.     }
    13. }
    14.  
    15.  
    16. void OnTriggerEnter(Collider other)
    17. {
    18.     insideTrigger = true;
    19. }
    20.  
    21. void OnTriggerExit(Collider other)
    22. {
    23.     insideTrigger = false;
    24. }
     
    erenvbn and SinaBanana1 like this.
  3. SinaBanana1

    SinaBanana1

    Joined:
    Oct 20, 2019
    Posts:
    8
    Hello we love you!!!!!
    You just helped something we were working on for two days- thank you so much.



     
    erenvbn and Joe-Censored like this.