- Home /
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?
Answer by OperationDogBird · Aug 11, 2012 at 09:43 AM
rigidbody.constraints can only be compared to RigidbodyConstraints.
So if you want to check if all are true, you would say something like
if(rigidbody.constraints==RigidbodyConstraints.FreezeAll)print("Frozen");
same thing for all the other RB Constraint values.
Also, in an if statement you need to say && not &.
Answer by AlexMiller12 · Aug 15, 2015 at 01:08 AM
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
I was thinking about how to test for an individual constraint regardless of others, this is exactly what I needed. Thanks!