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

LayerMask, Add Another Layer After Run

Discussion in 'Scripting' started by renman3000, Aug 2, 2014.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Can I alter the values excepted by a LayerMask post run?


    So, in the inspector I set LayerMask to except thisLayer, but after run, can I get it to accept thisLayer and thatLayer??
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    LayerMask both = maskA | maskB
     
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Is that actual code?
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    just missing a ;
     
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    I feel my chain is being pulled for some reason.

    Away from ' puter.



    Usually a Class object would be assigned in code as....
    Code (csharp):
    1.  
    2. int[] thisInt = new int[2]{1,2};
    3.  


    The reason I say an int array is that my layerMask, toting multiple layers is in itself an array.
     
  6. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    layer += newLayer; simple as that. BUT you have to make sure that the layer you want to add does not already exist or else the value of the layer can be different for instance. Suppose you wanted to add a PlayerLayer to a gameObject at run time, now suppose PlayerLayer is 3. Well, if the layer does not already exist layer+=PlayerLayer would be valid and add 3 to the Layer index. BUT if PlayerLayer 3 already exists, layer+=PlayerLayer will result in 3*3 which will be altered to 9. So, if there IS a layer with layer value 9 that will get added BUT if 9 does not exists it will throw an out of bounds exception. I know this because I built my own UI and I wanted to alter the camera layer at run time and I know how Layers work now so yeah if you need any help just let me know. :)
     
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    No, you cannot just add layermasks, because they are bitmasks, so you must use bitwise operators to combine them properly.

    And layermask isn't a class (or an array...), its a value, so you don't have to 'new' it.
     
    Last edited: Aug 3, 2014
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    When I tried

    thisLayerMask += 13; I got every layer, that is defined, added to the layerMask.

    When I did this...
    thisLayerMask += "thisLayer";, i get an error pre run.


    How do you insure layer 10, thisLayer, is added and none else?
     
  9. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Layermask newLayers = oldLayers | (1<<10);
     
  10. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Ok,
    So what is that line telling the computer exactly?
     
  11. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  12. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    So would (1<<11) set the layer mask to take in layer 11?
     
  13. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    (1<<11) is a mask for 'only layer 11'
    bitwise OR | with some otherMask to include 11 with the layers of otherMask
     
  14. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    So, in this case, what is old layers? For instance if I originally only had layer 10, or thatLayer activated.
     
  15. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    wow... handholding much?
    oldLayers is whatever layermask you have that you want to add layer 10 to.
    If you are setting layers in the inspector then its probably something like
    Code (CSharp):
    1. public LayerMask myMask;
    And then you are probably doing somthing like
    Code (CSharp):
    1. physics.raycast(ray, distance, myMask)
    But, myMask only has layers 8 and 9 set, so you want to add layer 10 into it, so you do
    Code (CSharp):
    1. myMask = myMask | (1<<10)
     
  16. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Thanks mom.

    ::
    Really appreciate it.
    I have never done this before and unfamiliar with the bit operations. What other areas that are common in Unity might I use them?


    And what yah making for dinner? I hope its spaghetti!
     
  17. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    It's a Sunday, Sunday is always a roast.

    Layermasks are also used for camera culling.
    But bitwise math can be used for quite a few things, hashing, psudorandom generators, bit flags
     
  18. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Hm.
    Interesting.


    Can't wait.
    Can I bring a friend??
     
  19. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    How do you properly remove a layer?
    I had been using this, but it is not working.

    Code (csharp):
    1.  
    2. layerMask ^= (0<<10);
    3.  
    Thanks
     
  20. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    ^ is XOR, which isnt what you want. Better to use AND NOT

    layermask = layermask & ~(1<<x)

    ps. 0<<10 is still 0
     
  21. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Hm.
    Ok, thanks.