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. Dismiss Notice

Question Physics.Raycast LayerMask

Discussion in 'Physics' started by dnzblg58, Jun 3, 2023.

  1. dnzblg58

    dnzblg58

    Joined:
    Jul 6, 2018
    Posts:
    8
    Hi guys, I have a problem with raycast. I have buttons on the screen of my game, even though I use raycast, the ray goes to them.

    Code (CSharp):
    1. void HitFire()
    2.     {
    3.         Ray _ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.         int layerMask = 1 << 5;
    5.         layerMask = ~layerMask;
    6.         //LayerMask layerMask = ~LayerMask.GetMask("UI");
    7.         if (Physics.Raycast(_ray, out RaycastHit hit,100f,layerMask))
    8.         {
    9.             if (hit.collider.gameObject.CompareTag("spider"))
    10.             {        
    11.                 Instantiate(BloodHitEffect, hit.point, Quaternion.identity);
    12.             }
    13.         }
    14.     }
    Layers
    ffff.jpg

    EDIT: I checked this function with
    Code (CSharp):
    1. if(EventSystem.current.currentSelectedGameObject == null)
    and problem solved. I've also given a button component to my UI elements in my game scene.

    NOT:
    Code (CSharp):
    1. if(EventSystem.current.currentSelectedGameObject == null)
    Instead of checking
    Code (CSharp):
    1. if(!EventSystem.current.IsPointerOverGameObject())
    you can also check
     
    Last edited: Jun 4, 2023
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,242
    Maybe make a LayerMask variable and set it in editor so you can visualize it's variable?
     
  3. dnzblg58

    dnzblg58

    Joined:
    Jul 6, 2018
    Posts:
    8
    @DevDunk i tried but it didn't work
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,242
    What is the collider?