Having trouble with CollisionFlags

I need to check if the collision flags for the sides of the controller are being raised:

if (controller.collisionFlags  CollisionFlags.Sides)
            print("Touching sides!");

When I try to run this code, however, I get the compile-time error "Cannot implicitly convert type UnityEngine.CollisionFlags' to bool’ ". The only thing I could think of was that it was confusing the “” operator with the “” operator, which doesn’t make sense of course. I don’t fully understand what “” does but I know that it’s checking a value in a bitmask.

Even when I just copied the script from the documentation page directly into a new script, I still got this error.

The documentation page, for reference:

In C# you’d have to do

if ( (controller.collisionFlags  CollisionFlags.Sides) != 0 )

I guess their JS code converter doesn’t deal with that sort of thing well.

–Eric

Thank you very much. I guess now would be a good time for me to go learn how to properly use bitmasks.

If collisionflags was let’s say Below (which is 4, or 00000100), and since Sides is 1 or 00000001, then you have

  00000100  Below
 00000001  Sides
----------
  00000000

That’s because only results in 1 if both matching bits are 1. If collisionflags had both Below and Sides, then you’d have

  00000101  Below and Sides
 00000001  Sides
----------
  00000001

Or if you were comparing against Below, then it would be

  00000101  Below and Sides
 00000100  Below
----------
  00000100

The result is therefore not 0 in both cases. (Also anything non-zero is “true” when implicitly cast to a boolean in JS like in the doc’s example code.)

–Eric

Wow, thanks again. :smile: That’s very good information.