Search Unity

Colliders to hide object

Discussion in 'Scripting' started by Afrokid001, Aug 18, 2013.

  1. Afrokid001

    Afrokid001

    Joined:
    Jun 3, 2010
    Posts:
    89
    I am currently trying to make an object disappear when it collides with another object and come back when it is no longer colliding.

    The object to I am wanting to collide with the disappearing objects is a child of the main camera. From what I am aware, to have a working collision code one object needs a rigidbody. However, when I attach one to the "CamDetect" object it bounces around and doesn't move with the camera properly. And when I attach it to the object I want to disappear, the object falls through the floor.


    The code as far as I know is sound, How would I get around this problem?

    While typing this up, I was thinking that maybe a raycast could work, I don't have much experience with raycasts and wouldn't know where to start.

    My code that I have on the disappearing item:

    Code (csharp):
    1. function OnTriggerEnter(col:Collider)
    2. {
    3.     if (col.tag == "CamDetect")
    4.     {
    5.         GetComponent(MeshRenderer).enabled = false;
    6.           Debug.Log("Collision");
    7.     }
    8. }
    9.  
    10. function OnTriggerExit(col:Collider)
    11. {
    12.     if (col.tag == "CamDetect")
    13.     {
    14.         GetComponent(MeshRenderer).enabled = true;
    15.           Debug.Log("No Collision");
    16.     }
    17. }
     
  2. Kray-C

    Kray-C

    Joined:
    Apr 8, 2013
    Posts:
    152
    Falling through the floor means that your floor has no collider.
    If you check the istrigger checkbox in the collider you should not have physical reactions.
    And if you do not need gravity you can deactivate it in the rigidbody.
     
  3. Afrokid001

    Afrokid001

    Joined:
    Jun 3, 2010
    Posts:
    89
    Oh, Thanks. I always seem to miss the simple things.

    It works now but for some reason the "function OnTriggerExit(col:Collider)" doesn't make it re-appear?

    Edit; added a rigidbody to both and now it works. Thanks again.
     
    Last edited: Aug 18, 2013