Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Working with Layers question.

Discussion in 'Scripting' started by jmattarock_unity, May 12, 2024.

  1. jmattarock_unity

    jmattarock_unity

    Joined:
    Mar 10, 2018
    Posts:
    28
    Not sure if this is a bug or I am missing something very obvious lol.

    I have his line of code:
    Code (CSharp):
    1.        
    2.  if (Physics.Raycast(_weaponSelected.spawnPoint.transform.position,  _weaponSelected.spawnPoint.TransformDirection(Vector3.forward), out hit, 10f))
    3.      
    4. if (hit.transform.gameObject.layer == attacklayer) {
    For some reason the last line of code never recognizes what is set in the attacklayer. Which I have set to "Enemy" in the inspector. I even debug.log the layer being hit by the Raycast and it comes up as layer 9 which is indeed the enemy layer. don't get any errors or warnings.

    but now if I use
    Code (CSharp):
    1.  if (hit.transform.gameObject.layer != LayerMask.NameToLayer("Enemy"))
    It works perfectly fine, I'm trying to stay away from hardcoding strings like that for obvious reasons.

    is it a bug or am i missing something?
     
  2. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    284
    First, simply output all incoming data to the console and inspect their values:

    Code (CSharp):
    1. Debug.Log(hit.transform.gameObject.layer);
    2. Debug.Log(attacklayer.value);
    3. Debug.Log(attacklayer);
    Mask layers are bitwise values, not just some integer ordinal number. Therefore, one way is to convert it into a bit mask:
    Code (CSharp):
    1. if (1 << hit.transform.gameObject.layer == attacklayer.value)
    2. {
    3.  
    4. }
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,219
    While samana's solution works for a layermask with a single layer, when you select multiple layers, this would not work. Here you should do the usual bitwise operation to check if a particular bit is set in the mask.

    Code (CSharp):
    1. int mask = 1 << hit.transform.gameObject.layer;
    2. if ((attacklayer.value & mask) != 0)
    With this approach you can select as many layers in your attacklayer Layermask variable and any specified would work. It's important to use the bitwise "and"
    &
    here, not the logical boolean "and"
    &&


    So say the layer in question is layer with index "5". When we do 1<<5 we get the value of 32 or in binary
    0x00100000
    . When you select layer 5 and 7 in your attacklayer mask, it would look like this
    0x10100000


    So when you "and" them together you get:
    Code (CSharp):
    1. 0x00100000
    2. 0x10100000
    3. ----------
    4. 0x00100000
    which gives you a result that is not 0. When the incoming layer would be layer 6, the mask would be
    0x01000000
    and the result of the and operation would be:
    Code (CSharp):
    1. 0x01000000
    2. 0x10100000
    3. ----------
    4. 0x00000000
     
    Last edited: May 14, 2024
    samana1407 likes this.
  4. jmattarock_unity

    jmattarock_unity

    Joined:
    Mar 10, 2018
    Posts:
    28
    Thanks for your help, for some reason though I get this error (Operator '&' cannot be applied to operands of type 'int' and 'bool') using this approach.
     
    Bunny83 likes this.
  5. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    227
    You'll need to add parenthesis around the bitwise operator.

    Code (CSharp):
    1. if ((attacklayer.value & mask) != 0)
     
    Bunny83 and orionsyndrome like this.
  6. jmattarock_unity

    jmattarock_unity

    Joined:
    Mar 10, 2018
    Posts:
    28
    Thanks Guys this worked perfectly :D
     
    Bunny83 likes this.
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,220
    Maybe you'll find this crash course on layers and bit masks useful.
     
    Bunny83 likes this.
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,219
    Yes, my bad :) I've written the answer right here without checking. I've fixed my post so nobody copies broken code from here.