K
Spare popcorn and coke. It'll be a long time, but it's fabulous. )What's that for?♪ Unityas we all know (or not only all) there are layers (layers). To refresh the memory that this is (and, if you don't know, read and see) can be found https://docs.unity3d.com/ru/530/Manual/Layers.html ♪ They use them differently. For example 2D They can be identified and separated by a rear/medium/transition plan. Something in the back, something in the front. In more complex layer games there may be more layers and objects can be grouped into these layers. For example, a layer can be made for NPC and to make it an enemy layer. NPC ♪ Enemies and others.In general, manipulation from layerMask (Mask) determines whether the audited object (GameObject) is located in the said layer or not (whether this is for the object and how it is checked). Depending on the result of the verification, make the intended actions. Let's see, for example, Raycast♪ If we turn to https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html You can see the method looks like:public static RaycastHit2D Raycast(Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);
One of the parameters: int layerMask = DefaultRaycastLayers♪ It operates as follows: a beam is produced from a point in some direction (tolerance this magic at a distance), the beam falls on a barrier (object) and, for example, if the object is in a layer Enemies - Smash him. Examples (Collisionmask.value & (1 << layer)) == 0 I'll talk to you later. But the point is, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, like, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, like, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you ♪I hope the point is clear, let's go on. How it works.Number of sections Unity32. They're numbering from 0 to 31. LayerMask (mass) - appears to be a 32-bit whole number:0000 0000 0000 0000 0000 0000 0000 0000
I've separated every four bats for better readability. The mask uses its 32 bats to present the different https://ru.wikipedia.org/wiki/%D0%A4%D0%BB%D0%B0%D0%B3_(%D0%BA%D0%BE%D0%BC%D0%BF%D1%8C%D1%8E%D1%82%D0%B5%D1%80%D0%BD%D0%B0%D1%8F_%D1%82%D0%B5%D1%85%D0%BD%D0%B8%D0%BA%D0%B0) (Status of object, e.g.: 1 - on, 0 - off). In this particular case, each flag(s) is a specific layer. We'll take the first eight layers and record each layer's dual performance.The first battle is on the right side.)слой № Двоичное Десятичное
Layer 0 = 0000 0001 = 1
Layer 1 = 0000 0010 = 2
Layer 2 = 0000 0100 = 4
Layer 3 = 0000 1000 = 8
Layer 4 = 0001 0000 = 16
Layer 5 = 0010 0000 = 32
Layer 6 = 0100 0000 = 64
Layer 7 = 1000 0000 = 128
That's what the layers represent in a two-way show. The specific layer appears to be defined by one specific bat. Now we're trying to figure this out. if ((Collisionmask.value & (1 << layer)) == 0)♪We see the variable. layerwhich is a number. This variable-- Class number (0, 1, 2, 3, 4...) Not at all. 10th and Not at all. It's a double performance. Collisionmask The inspector is a falling list with layers: A Collisionmask.value In turn, binary presentation a combination of selected layers (as more than one and several layers can be chosen). To understand, for example, if we want to create a mask for layers #2 and #5, then a double vision will be:0010 0100 // #2,#5
or, for example, layers #1, #2, #5, #6 will represent:0110 0110 // #1,#2,#5,#6
What do we want? We want to know if this mask is part of the layer we're looking into. layer? And depending on that, do something. In the example above, we want to enter a condition and do something if our facility is in layer 5, Not It's in that mask. Let right. LayerMask = 0110 0110; // #1,#2,#5,#6And the number of our layer int layer = 5;We need layer 5 to lead to a two-way representation that matches the layers. Let's see the sign up. Glass 5 has a decimal representation of 32. It is, in turn, a dual-track feature of 0010,000.
It is produced by the use of a rotating operator to the left << for the youngest bita itself (00000,0001) for 5 values (fantastics). Let's move the line five times to the left: As we can see, the number 0010,000 is really on our mask. 0110 0110because the mask contains a single in position number 6 to the right, and layer 5 has a single in the same position. If you don't believe in the word, it's easy to check: you have to multiply (small and derivative) between the mask and the value to be checked. It's, by the way, in the line. Collisionmask.value & (1 << layer)0110 0110 // #1,#2,#5,#6
& (and)
0010 0000 // #5
0010 0000
We see what's in the mask. If he hadn't come in, a broken multiply would have resulted in 0. As an example of evidence, we can check whether or not Style 7 enters this mask. Let's move the junior battle to seven positions to the left: We get 1,000 0000. Check. 0110 0110 // #1,#2,#5,#6
& (and)
1000 0000 // #7
0000 0000
We see 0. Not in. And if it doesn't fit, it makes our condition on top: if ((Collisionmask.value & (1 << layer)) == 0) => if (0 == 0) => true
So we're in a condition and doing what we need. That's what we were looking for.Supplement 1In addition to checking the specific layer for entering the mask, all layers can be checked, Except specified. We just need to use the inversion operator.
He's changing the battling of Operanda 0 by 1 and one by 0. So, 6,00000010 will be 11111101, which will test every layer except #2.
If you want to delete a few layers, you'll have to combine the masks through the O.R.int layer1 = 3;
int layer2 = 5;
int layermask = ~((1 << layer1) | (1 << layer2)) // НЕПРАВИЛЬНО: ~(1 << layer1) | ~(1 << layer2)
Supplement 2In fact, you don't always have to write. 1 << что-то♪ For example Raycast You can pass the parameter. Collisionmask.valuewhere Collisionmask Type https://docs.unity3d.com/ScriptReference/LayerMask.html and, in turn, the inspector looks like a falling list (see image No.2). Very convenient.There's no need to harbor numbers, that's not to write. 1 << 7 or 1 << 5'Cause you can forget it's five and seven. In order to know the number of layers, you can use https://docs.unity3d.com/ru/current/ScriptReference/LayerMask.NameToLayer.html example:int npcLayer = LayerMask.NameToLayer("NPCLayer");
int npcMask = 1 << npcLayer;
And it's the same with the group masks:int npcGoodLayer = LayerMask.NameToLayer("NPCGood");
int friendsLayer = LayerMask.NameToLayer("Friends");
int npcGoodMask = 1 << npcGoodLayer;
int friendsMask = 1 << friendsLayer;
int finalmask = npcGoodMask | friendsMask; // Or, (1 << npcGoodLayer) | (1 << friendsLayer)
Supplement 3It's actually a design. 1 << ... He's not only in Unity. This is a well-known practice in programming. For example, a producer has a product, an annex and it provides API for other programmers.API is a set of ready classes, procedures, functions, structures and constants provided by an annex (library, service) or an operating system for use in external programme products. Used by programmers in writing all possible applications. (c) WikipediaAPI may have restrictions on access of different kinds. The software tells the mask where he wants access, and the application server checks this and, in the case of a mask discrepancy, gives a warning or something.
The simplest example of Wontact and his There's a decimal idea, but it doesn't change the point.