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

Click object behind other object

Discussion in 'Scripting' started by dontr, Jun 30, 2017.

  1. dontr

    dontr

    Joined:
    Jun 29, 2017
    Posts:
    8
    Hello guys. I have a small problem.
    I have an object A rolling over object B. When i press on B i want to delete object A. The script is working but the problem is that sometimes i cant click on object B because A is all over it. So bassically object A is in the way of object B. How can i click through A?


    Code (CSharp):
    1. List<GameObject> currentCollisions = new List<GameObject>();
    2.  
    3. // Add the GameObject collided with in a list when entering
    4.     void OnTriggerEnter(Collider col)
    5.     {
    6.         currentCollisions.Add(col.gameObject);
    7.      
    8.     }
    9.  
    10. // Remove the GameObject from the list on exit
    11.     void OnTriggerExit(Collider col)
    12.     {  
    13.         currentCollisions.Remove(col.gameObject);
    14.    }
    15.     // When clicking on object B all object above it get destroyed
    16.     void OnMouseDown()
    17.      {
    18.          if (gameObject.tag == "cubes")
    19.              foreach (GameObject g in currentCollisions)
    20.              {
    21.                  Destroy(g);
    22.              }
    23.  
    24.      }
    Thank you.
     
  2. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    Hey, instead of OnMouseDown try and Use Physics.Raycast or Physics.RaycastAll in a manager object that is not directly related to the snowballs (this script is not a component of the snowball object). To use raycasts you need a ray. The best way to get a ray in your situation is to use Camera.ScreenPointToRay. Here is a tutorial that explains the entire raycast processes:



    PS: if you want to use the script on PC use Input.mousePosition and Input.GetMouseButtonDown

    Now, a normal raycast will retrieve the first object that the ray meets on the predefined layer. To get all the objects (both the snowball and the object behind) you use RaycastAll as described in the documentation example
     
    cstooch likes this.
  3. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    You can also supply a layermask parameter in a Raycast... if object b and object a can be on different layers. A layermask allows you to ignore all objects on a layer you choose.

    The easiest way to do that is to declare the layermask variable as public, then check/uncheck the layers you want in your raycast as needed.
     
    CloudKid likes this.
  4. dontr

    dontr

    Joined:
    Jun 29, 2017
    Posts:
    8
    Yep. All done. Thank you guys ^^
    Here is the script:
    Code (CSharp):
    1. List<GameObject> collisionList = new List<GameObject>();
    2.  
    3.     void Update()
    4.     {//Detecting the click on gameobject with tag "cube"
    5.         if (Input.GetMouseButtonDown(0))
    6.         {
    7.             RaycastHit hit;
    8.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    9.             if (Physics.Raycast(ray, out hit))
    10.            
    11.                 if (hit.transform.gameObject.tag == "cubes")
    12.                 {
    13.                     foreach (GameObject z in collisionList)
    14.                     {
    15.                         Destroy(z);
    16.                     }
    17.                 }
    18.                  }
    19.     }
    20.  
    21.     // Add the GameObject collided with, in a list, when entering
    22.     void OnTriggerEnter(Collider col)
    23.     {
    24.         collisionList.Add(col.gameObject);
    25.     }
    26.  
    27.    
    28.     // Remove the GameObject from the list on exit
    29.     void OnTriggerExit(Collider col)
    30.     {
    31.         collisionList.Remove(col.gameObject);
    32.    }
     
    TaleOf4Gamers likes this.
  5. OwenKuhn

    OwenKuhn

    Joined:
    Aug 14, 2013
    Posts:
    1
    For anybody who's looking at this for new answers, switching object A's layer to "Ignore Raycast" should make it so that mouse clicks and finger taps register whatever is behind it instead. (this only works if you NEVER want to click on A for any reason)
     
  6. Gileno

    Gileno

    Joined:
    Nov 23, 2018
    Posts:
    29
    Ty @OwenKuhn.

    This is exactly what i was looking for.