When you add another one you get a carry into the 10s place and the ones go back to 0. When you count in a number system you generally start at the ones place and go through the available digits. ), in binary each digit grows by a factor of 2. While in the decimal system each digit place is the value of the previous place multiplied by 10 (so "ones" at the far right, then 10s, 100s, 1000s. That changes the "value" of a certain digit position. All those number systems actually work exactly the same, though they have different number of symbols per digit. It allows you to view numbers as hexadecimal (base 16), decimal (our usual base 10 system), octal (base 8) or binary (base 2). So every bit that was originally 0 becomes a 1 and every 1 becomes a 0. The bitwise "not" operator ~ simply inverts all bits. When using the proper bitwise or operator, each bit is "or"ed with the corresponding bits. Though our two initial masks contained layer 0 and 1. When you just "add" them up (33 + 3 = 36) you get a mask that looks like this: 0010 0100. Just as an example, imagine the layermasks "33" (0010 0001, so layer 0 and layer 5) and the layermask 3 (0000 0011, so layer 0 and layer 1). ![]() To combine several bitmasks into one, you should use bitwise operators like or |, and & as well as not ~ While you could simply "add" several layer mask values up (1 + 8 = 9) it is not recommended as when you try to combine two layer masks that have "overlaps", you get a "carry" and it results in the wrong bits being set. ![]() So by using the number 1 and shifting it x times to the left, you get the value for only layer x. With the "bitshift left" operator you can easily get the actual mask value for a certain layer since the bit shift simply shifts the bits in the given number by x bits to the left. Layer 0 has the value 1 (= 1 << 0) while layer 3 has the value 8 (= 1 << 3). When you would use the decimal number "9" as a layermask, you would actually use a mask like this:Ġ000 0000 0000 0000 0000 0000 0000 1001 which is binary for "9" is only includes layer 0 and layer 3. ![]() So all 32 bits are set, except bit number 2 (the third layer since we start counting at 0) which represents the ignore raycast layer. The binary number I just showed is the actual value of "DefaultRaycastLayers". The default layer masks are defined here. To visualize a bit mask, we can write the number as a binary number like that A bit can either be set (so it's 1) or clear (so the bit is 0). ![]() We already have countless of other posts that explain this, but I think there's always someone that finds this thread before any other, so it doesn't hurt.Ī 32 bit integer, as the name suggests, has 32 bits. So a mask of "0" would mean you can't hit anything while "-1" (equivalent to 0xFFFFFFFF) means all 32 layers can be hit, even the "ignore raycast layer" which is usually excluded in the Physics.DefaultRaycastLayers ^^. Though a 1 in the mask means you can hit the layer, a 0 at the corresponding bit position means you can not hit that layer. Because he explained it the other way round Yes, a mask is masking layers which you want / don't want to hit. Click to expand.Right, I guess Kurt had something mixed up when he wrote his answer back then.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |