Search Unity

Problems with Layer Mask

Discussion in 'Physics' started by David42142, Sep 2, 2020.

  1. David42142

    David42142

    Joined:
    Jul 15, 2015
    Posts:
    13
    Physics.Raycast(ray, out hit, 50)

    This raycast collides with a test object, but also collides with the player which is undesired.


    Physics.Raycast(ray, out hit, 50, LayerMask.NameToLayer("Player"))

    This one ignores the player but also ignores the test object. The test object is not using the player layer.

    I am so confused
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    The argument there is a bit-mask where each bit represents a layer, it's not a single layer.

    Try LayerMask.GetMask

    Effectively it's like doing:
    Code (CSharp):
    1. Physics.Raycast(ray, out hit, 50, 1 << LayerMask.NameToLayer("Player"))
     
    ArshakKroyan likes this.