Skip to main content
added 664 characters in body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27

Using bit shifting allows you to take into account multiple layers in one physics operation:

 Physics.Raycast(ray, out hitInfo, Mathf.Infinity, layerMask ) 

Without bit shifting, you would be allowed to raycast in one layer and only one. While with bit shifting, you can raycast in multiple specific layers:

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; 

You can also raycast in every layers except specific ones :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; layerMask = ~layerMask; 

If you look at the "Layer manager" in Unity, layers can be seen as the indices of a simple one-dimension array.


EDIT : I've never seen it before, but the LayerMask class has a utility function to get the "computed" layer mask given the layers names :

Debug.Log( LayerMask.GetMask("UserLayerA", "UserLayerB") ) ; 

Suppose UserLayerA and UserLayerB are the tenth and eleventh layers. These will have a User Layer values of 10 and 11. To obtain their layer mask value their names can be passed into GetMask. The argument can either be a list of their names or an array of strings storing their names. In this case the return value will be 2^10 + 2^11 = 3072.

Link to documentation : https://docs.unity3d.com/ScriptReference/LayerMask.GetMask.html

Using bit shifting allows you to take into account multiple layers in one physics operation:

 Physics.Raycast(ray, out hitInfo, Mathf.Infinity, layerMask ) 

Without bit shifting, you would be allowed to raycast in one layer and only one. While with bit shifting, you can raycast in multiple specific layers:

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; 

You can also raycast in every layers except specific ones :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; layerMask = ~layerMask; 

If you look at the "Layer manager" in Unity, layers can be seen as the indices of a simple one-dimension array.

Using bit shifting allows you to take into account multiple layers in one physics operation:

 Physics.Raycast(ray, out hitInfo, Mathf.Infinity, layerMask ) 

Without bit shifting, you would be allowed to raycast in one layer and only one. While with bit shifting, you can raycast in multiple specific layers:

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; 

You can also raycast in every layers except specific ones :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; layerMask = ~layerMask; 

If you look at the "Layer manager" in Unity, layers can be seen as the indices of a simple one-dimension array.


EDIT : I've never seen it before, but the LayerMask class has a utility function to get the "computed" layer mask given the layers names :

Debug.Log( LayerMask.GetMask("UserLayerA", "UserLayerB") ) ; 

Suppose UserLayerA and UserLayerB are the tenth and eleventh layers. These will have a User Layer values of 10 and 11. To obtain their layer mask value their names can be passed into GetMask. The argument can either be a list of their names or an array of strings storing their names. In this case the return value will be 2^10 + 2^11 = 3072.

Link to documentation : https://docs.unity3d.com/ScriptReference/LayerMask.GetMask.html

added 15 characters in body
Source Link
user1430
user1430

Using bit shifting allows you to take into account multiple layers in one Physicsphysics operation.:

 Physics.Raycast(ray, out hitInfo, Mathf.Infinity, layerMask ) 

Without bit shifting, you would be allowed to raycast in one layer and only one. While with bit shifting, you can raycast in multiple specific layers  :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; 

You can also raycast in every layers exceptexcept specific ones :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; layerMask = ~layerMask; 

If you look at the "Layer manager" in Unity, layers can be seen as the indices of a simple one-dimension array : https://docs.unity3d.com/Manual/Layers.html"Layer manager" in Unity, layers can be seen as the indices of a simple one-dimension array.

Using bit shifting allows you to take into account multiple layers in one Physics operation.

 Physics.Raycast(ray, out hitInfo, Mathf.Infinity, layerMask ) 

Without bit shifting, you would be allowed to raycast in one layer and only one. While with bit shifting, you can raycast in multiple specific layers  :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; 

You can also raycast in every layers except specific ones :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; layerMask = ~layerMask; 

If you look at the "Layer manager" in Unity, layers can be seen as the indices of a simple one-dimension array : https://docs.unity3d.com/Manual/Layers.html

Using bit shifting allows you to take into account multiple layers in one physics operation:

 Physics.Raycast(ray, out hitInfo, Mathf.Infinity, layerMask ) 

Without bit shifting, you would be allowed to raycast in one layer and only one. While with bit shifting, you can raycast in multiple specific layers:

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; 

You can also raycast in every layers except specific ones :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; layerMask = ~layerMask; 

If you look at the "Layer manager" in Unity, layers can be seen as the indices of a simple one-dimension array.

edited body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27

Using bit shifting allows you to take into account multiple layers in one Physics operation.

 Physics.Raycast(ray, out hitInfo, Mathf.Infinity, layerMask ) 

Without bit shifting, you would be allowed to raycast in one layer and only one. While with bit shifting, you can raycast in multiple specific layers :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) +| (1 << LayerMask.NameToLayer("MyLayer2")) ; 

You can also raycast in every layers except specific ones :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) +| (1 << LayerMask.NameToLayer("MyLayer2")) ; layerMask = ~layerMask; 

If you look at the "Layer manager" in Unity, layers can be seen as the indices of a simple one-dimension array : https://docs.unity3d.com/Manual/Layers.html

Using bit shifting allows you to take into account multiple layers in one Physics operation.

 Physics.Raycast(ray, out hitInfo, Mathf.Infinity, layerMask ) 

Without bit shifting, you would be allowed to raycast in one layer and only one. While with bit shifting, you can raycast in multiple specific layers :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) + (1 << LayerMask.NameToLayer("MyLayer2")) ; 

You can also raycast in every layers except specific ones :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) + (1 << LayerMask.NameToLayer("MyLayer2")) ; layerMask = ~layerMask; 

If you look at the "Layer manager" in Unity, layers can be seen as the indices of a simple one-dimension array : https://docs.unity3d.com/Manual/Layers.html

Using bit shifting allows you to take into account multiple layers in one Physics operation.

 Physics.Raycast(ray, out hitInfo, Mathf.Infinity, layerMask ) 

Without bit shifting, you would be allowed to raycast in one layer and only one. While with bit shifting, you can raycast in multiple specific layers :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; 

You can also raycast in every layers except specific ones :

layerMask = (1 << LayerMask.NameToLayer("MyLayer1")) | (1 << LayerMask.NameToLayer("MyLayer2")) ; layerMask = ~layerMask; 

If you look at the "Layer manager" in Unity, layers can be seen as the indices of a simple one-dimension array : https://docs.unity3d.com/Manual/Layers.html

Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27
Loading