Assigning multiple layers to a Layer Mask through code?
I know that you can set up a Layer Mask through code with the LayerMask.NameToLayer
function, and that public layer masks can be set through a dropdown in the editor.
However, a bug in the current version of Unity (5.3.5) is causing my editor to crash on close, and it periodically resets my public layer masks. Seems like a good opportunity to set up these masks in code once and for all - except I'm not sure how.
I suppose the 1 << LayerMask.NameToLayer
command takes the layer's int and converts it into a bit value that the layer mask can understand, but how would I give it more than one value?
Answer by jgodfrey · Jun 30, 2016 at 12:30 AM
See the "Combining layermasks" section in the accepted answer here:
http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html
Answer by AstroKale · Jan 21 at 10:58 PM
While the previous linked answer works, it seemed a bit convoluted. This will work as well:
public LayerMask hitLayers = LayerMask.GetMask("Objects") | LayerMask.GetMask("Player") | LayerMask.GetMask("Terrain");
Keep in mind that the "text" within the GetMask() call must be an existing layer. You could avoid typos by having a static string OBJECTS_LAYERMASK_NAME.