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

Physics OverlapSphere layermask not working?

Discussion in 'Scripting' started by IamKevinRichardson, Feb 17, 2020.

  1. IamKevinRichardson

    IamKevinRichardson

    Joined:
    Feb 5, 2014
    Posts:
    52
    Can someone please help

    Following Code

    Code (CSharp):
    1. GameObject[] possibleTargets = System.Array.ConvertAll(Physics.OverlapSphere(transform.position, sightDistance, 8), col => col.gameObject);
    only works when I don't use a layermask... the 8.

    I made sure to create a new layer at the 8th place and apply it to the GameObjects I want involved... but it does not return any GameObjects
     
  2. IamKevinRichardson

    IamKevinRichardson

    Joined:
    Feb 5, 2014
    Posts:
    52
    When I try it with layermask 9 it returns a lot of gameobjects, even those not in layer 9...
     
  3. IamKevinRichardson

    IamKevinRichardson

    Joined:
    Feb 5, 2014
    Posts:
    52
    Zynek likes this.
  4. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    In Unity you have 32 layers. The layer mask lets you choose any combination of these layers.
    In C#, the int type is 32 bits. Each bit corresponds to a layer.
    By doing 1 << 9, you are setting only bit 9, and therefore mask out all other layers except 9.

    If you use int.MaxValue as your layer mask, all 32 layers will be cast against because all 32 bits are set.
     
    jchambers and IamKevinRichardson like this.
  5. IamKevinRichardson

    IamKevinRichardson

    Joined:
    Feb 5, 2014
    Posts:
    52
    Wow thank you!
    Just to try and clarify,
    1) if I just type the number 11 in the layermask spot, it will include ALL layers up to 11? If so, why was 9 returning everything but 8 returning nothing?
    2) Can I choose different layers by doing something like 1<<3<<6<<7 ?
     
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    1) Nope, that's not how binary works. Each bit in binary is a power of two.
    1 2 4 8 16 32 64 128 256 512 (etc)

    So the number "11" would be a combination of
    Bit 1 (1)
    Bit 2 (2)
    Bit 4 (8)
    1 + 2 + 8 = 11

    You'd be selecting layers 1 2 and 4 by using "11" here.

    2) You can use a binary or operator (|)
    ex. int mask = 1 << 2 | 1 << 3;
    This would choose layers 2 and 3.

    You can also use names if you'd like, by using LayerMask.GetMask
     
    Swearsoft and papafhill like this.
  7. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,365
    The layer mask parameter is expecting a bitmask, not a layer number, so simply passing in the number of a layer will not work.

    If you don't know how bitmasks work, the easiest thing to do is declare a public LayerMask variable as a class member, like so
    public LayerMask myLayerMask;

    Then pass myLayerMask in to OverlapSphere instead of 8. Unity will render a layer-selecting widget in the inspector, which you can use to select which layers you want and Unity will create the LayerMask value behind the scenes.