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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question OnTriggerStay bounds completely

Discussion in 'Scripting' started by ernov, Oct 7, 2020.

  1. ernov

    ernov

    Joined:
    Sep 17, 2014
    Posts:
    21
    Hello
    I would like my object "thing" to be related in my GameObject, but only if it is completely inside my GameObject.
    At the moment, it does detect when it is inside, but not the way I would like it to be.
    I tried with the min and max bounds, but it doesn't work, do you have an idea ?
    Thanks a lot!

    Code (CSharp):
    1. MeshCollider thing;
    2.  
    3.     private void Start(){
    4.         thing= GetComponent<MeshCollider>();
    5.          }
    6.  
    7. private void OnTriggerStay(Collider other){
    8.        if(other.gameObject.tag == "Mything"){
    9.  
    10.        /* Here it works */
    11.        //other.transform.parent = gameObject.transform;
    12.  
    13.        if(thing.bounds.min.x > other.bounds.min.x &&
    14.         thing.bounds.max.x < other.bounds.max.x &&
    15.         thing.bounds.min.y > other.bounds.min.y &&
    16.         thing.bounds.max.y < other.bounds.max.y &&
    17.             thing.bounds.min.z > other.bounds.min.z &&
    18.         thing.bounds.max.z < other.bounds.max.z){
    19.  
    20.              /* Here it doesn't work */
    21.                other.transform.parent = gameObject.transform;
    22.              
    23.                }
    24.  
    25.      }
    26.          
    27.  
    28. }
    29.  
    30. private void OnTriggerExit(Collider other){
    31.      if(other.gameObject.tag == "Mything"){
    32.       other.transform.parent = null;
    33.      }
    34.  
    35. }
     
    Last edited: Oct 7, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    I don't trust using the bounds for such a tightly-controlled thing. I have seen bounds be inexplicably not what I expect them to be at times, and there is no way for me to control them. They are also always axis-aligned, which kinda breaks a lot of other uses.

    To see what the bounds are during your run, print them out. I suspect you'll be surprised that they're not what you expect. You can also draw them in your scene with OnDrawGizmos() and see them realtime.

    Personally I would recommend using some other mechanism in your prefabs, such as setting up your own bounds, or sphere, or some kind of reliably way that you fully control rather than piggybacking on the collider bounds.
     
    ernov and PraetorBlue like this.
  3. ernov

    ernov

    Joined:
    Sep 17, 2014
    Posts:
    21
    Thank you very much for taking the time to respond.
    You're probably right, this story is not easy.