Can someone please explain why the first line works (returns true in raycast) while the second line does not (returns false in raycast)?

LayerMask tempMask = LayerMask.GetMask("Terrain");
LayerMask tempMask2 = LayerMask.NameToLayer("Terrain");


  • A Layer has an integer value and a name. So, say “Terrain” is Layer 10.

  • A LayerMask is a bitmask of one or more layers, which can also be expressed as an integer. The bitmask for layers 1 and 3, for example would be 00000101 (1st and 3rd bit from the right are set “on”, other bits are “off”) which is the same as the integer 5.

For raycasting purposes etc., you want the integer representing a layermask with the Terrain layer turned on, which is what GetMask gives you. Not the integer value of the terrain layer, which is what NametoLayer gives you.