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

Resolved Physics.Raycast ignoring layermask; hits every layer.

Discussion in 'Physics' started by ianandasher, Aug 27, 2023.

  1. ianandasher

    ianandasher

    Joined:
    Feb 3, 2021
    Posts:
    8
    Code (CSharp):
    1.             Ray ray = gameObject.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
    2.             if (Physics.Raycast(ray, out RaycastHit hitinfo, float.MaxValue,3))
    3.             {
    4.                 curm.movepos = new Vector3(hitinfo.point.x, curm.movepos.y,hitinfo.point.z);
    5.                 Debug.Log(hitinfo.point);
    6.             }
    This is my code.

    3 is supposed to be the layer mask, it is only supposed to hit on colliders with the third layer. But, no, it doesn't. It hits every single layer. The only way to get it to not do this is to remove the colliders on other objects. I don't know what's going on, there's nothing that should be doing this, its just another headache unity tries to give me.
     
  2. ianandasher

    ianandasher

    Joined:
    Feb 3, 2021
    Posts:
    8
    Pro tip to all: DONT enter integers as the layer mask, apparently it doesn't like to take in integers even though thats what the raycast says you can do, so, just put in a layermask
     
  3. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,715
    You can enter integers just fine, it's just that you're using it wrong (although your confusion is understandable!).

    As explained in the manual, layer masks are bitmasks. This means that each bit in the number you pass represents the layer at that bit's position. Passing "3" (011 in binary) means you're telling the raycast to collide with layers 0 and 1 (from right to left, bit #0 is enabled and bit #1 is enabled).

    More examples:
    Passing "5" (101) affects layers 0 and 2
    Passing "8" (1000) affects layer 3.
    Passing "15" (1111) affects layers 0,1,2 and 3.
    Passing "16" (10000) affects layer 4.

    And so on. I'd recommend reading about bitwise operators and bit manipulation in general to learn more. This is extremely common in programming, not just a Unity thing.

    cheers!
     
    Last edited: Aug 28, 2023
    halley likes this.
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,976
    Some quick shortcuts converting Layer Numbers to LayerMask Values and other operations.

    LayerMask mask = (1 << layerNumber);


    LayerMask mask = (1 << layerNumberA) | (1 << layerNumberB) | (1 << layerNumberC);


    LayerMask mask = (1 << layerNumber) | ((int)(layerMaskToInclude));


    LayerMask mask = ((int)(layerMaskToInclude)) & ~((int)(layerMaskToExclude));


    LayerMask mask = 0;   // no layers


    LayerMask mask = ~0;  // all layers
     
    tjmaul and arkano22 like this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,792