- Home /
Is there a way to stop a rigidbody from moving in a specific direction?
The way I would usually do it is by setting rigid.velocity = new vector3 and then zeroing out one of the directions I don't want the object to be able to continue in, but since the direction can be variable, I'm having trouble figuring out a way of doing this.
I started down this path after I had rigidbodies interacting in ways I didn't want, so I'm essentially forcing hovering to make sure they don't touch and so, don't interact. So I'm pursuing this for now.
Answer by JChilton · Feb 24, 2015 at 05:48 PM
Yes, these are the constraints. You can set them via inspector, or by code.
rigidbody.constraints = RigidbodyConstraints.FreezePositionX;
Use bitwise operands to combine contraints, if you wanted to freeze the X and Y you would use
rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY;
That only applies to the cardinal directions, so I can't use it since the direction that it might interact with a rigidbody could come from any angle.
If you're not wanting the rigidbodies to ever interact, you could set them as kinematic then apply a single Velocity. The colliders will still interact if they're set as triggers for logic purposes.
Your answer