How to constraint the motion of a rigid body to the xz plane?

Hi Guys,

I am working on a game similar to air hockey. I want to restrict the movement of puck to xz axis. Currently when i hit the puck it travels in y axis with some units too. but i dont want it to leave the base ever. plz help..................

Simply use the rigidbody.constraints syntax.
Note that if you are constraining in more than one dimension, you can’t just write a new line like this.

rigidbody.constraints = RigidBodyConstraints.FreezePositionY;
rigidbody.constraints = RigidBodyConstraints.FreezeRotationX;

the second line will overwrite the first, you have to use the vertical break (upright line key, don’t know the name) to combine constraints. In this case I would use three to keep a puck constrained to one plane and not have accidental rotations cause anything odd.

rigidbody.constraints = RigidBodyConstraints.FreezePositionY | RigidBodyConstraints.FreezeRotationX | RigidBodyConstraints.FreezeRotationZ ;

You could still allow rotation on the Y axis, leaving some flexibility for some fun skins that would still animate without anything too fancy.

you could use Rigidbody.constraints if you are using a rigidbody... Unity could not find RigidbodyConstraints when i tried it even though its there on unity site. Maybe you will have better luck in findng out how to use it. If you do use it, do share... you could use: rigidbody.constraints = RigidbodyConstraints.FreezePositionY;


You could add the following to the Update function in the script attached to your puck: `transform.position.y = 0;`

I know this is an old thread, but it comes up as a top result in a Google search.

Ok so in your case you have a hockey puck and a board. You don’t want the hockey puck to travel on the Y axis.

Using Unity 2017 for a 3D game…

I have found that RigidBodyConstraints don’t work that well.

I prefer instead to use a “ConfigurableJoint”. What I do is…

  • Create a GameObject at ground level, and call it hockeyBoard
  • Give it a Rigidbody set as Kinematic
  • Give it a ConfigurableJoint with “Y Motion” set to Locked
  • Drag your hockeyPuck to the “ConnectedBody” of your ConfigurableJoint

Now your hockeyPuck is constrained to the hockeyBoard, and can move along the X axis and Z axis, but not the Y axis.