Query Rigidbody Constraints?

How do I go about checking if a rigidbody contains (among others) a specific restraint? Supposedly, I should be using:

if (rigid.constraints & RigidbodyConstraints.FreezePosition)

However, this doesn’t work. I get an error telling me that rigidbody.constraints can’t be converted to a boolean.

Any ideas?

The marked solution is actually a little dangerous. If you want to know if the X axis is frozen, you may get undesirable behavior if other axes are frozen as well.

rigidbody.constraints = RigidbodyConstraints.FreezeAll;
bool isXFrozenBad = ( rigidbody.constraints == RigidbodyConstraints.FreezePositionX );
bool isXFrozenGood = ( rigidbody.constraints & RigidbodyConstraints.FreezePositionX ) != RigidbodyConstraints.None;

// isXFrozenBad is false, isXFrozenGood is true

rigidbody.constraints can only be compared to RigidbodyConstraints.

So if you want to check if all are true, you would say something like


same thing for all the other RB Constraint values.

Also, in an if statement you need to say && not &.