Hi! I don’t undertand this operation below:
grounded = (flags CollisionFlags.CollidedBelow) != 0;
What is this doing??? Very different value. I don’t undertand about that value in grounded var.
Hi! I don’t undertand this operation below:
grounded = (flags CollisionFlags.CollidedBelow) != 0;
What is this doing??? Very different value. I don’t undertand about that value in grounded var.
is bitwise and so it will return a number where all bits are set to 1 if the corresponding bit was set to 1 on both values you apply it to
e.g.,
00 01 10 11
11 10 11 01
-- -- -- --
00 00 10 01
You only get a 1 in the result if both bits are 1. So grounded will be true if “flags” masked with “CollisionFlags.CollidedBelow” is not zero. This is useful because “flags” is a single number containing information about above, sides, and below in different bits. You don’t care about the other values of CollisionFlags in this case (namely Sides and Above) so you use with CollisionFlags.CollidedBelow, and the result is non-zero regardless of whether the Above or Sides bits are set.
(Note that CollisionFlags.CollidedBelow should actually just be CollisionFlags.Below, and same with .Above…I think CollidedBelow is from the Unity 1.x days, but they haven’t removed the old syntax, and their character controller scripts still use it, even in the new Unity 3 CharacterMotor script.)
To use actual binary numbers: if set, CollisionFlags.Sides = 001, .Above = 010, and .Below is 100. If Below and Sides are true at the same time, you have 101. If all of them are true, you have 111. (If none, obviously, you have 000). So let’s say your sides are colliding, and you mask it with .Below:
001
100
---
000
You just get 0, and will only get 0 unless the .Below bit in flags is 1. For example, you’re colliding on the sides and below:
101
100
---
100
Therefore, grounded is true since the result isn’t 0. 100 in binary would be 4 in decimal numbers, but since you’re using enums, you don’t really care what the actual numbers are. Only if the result is 0 or not.
–Eric
I was understand a little about the functioning as Dreamora said, but the table by Eric5h5 help very much. So how would a binary compound of 1 and 0!? How works. Cool.
I believe it has almost the same function when I were using in C++.
Thanks friends for the replies!