- Home /
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..................
the "dirty" update version works, but did anybody figure out how these constraints actually work?
please post questions or comments as questions or comments, not answers :D
Quite simple. Have a look at rigidbody.constraints
To be specific to your question,
rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ;
Answer by Elroyman · Aug 09, 2013 at 01:06 AM
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.
constraints, not constrain lol XD But good answer overall, +1
Answer by Antzy · Feb 26, 2011 at 07:20 AM
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;
Source: http://unity3d.com/support/documentation/ScriptReference/Rigidbody-constraints.html
Answer by cjmarsh · Jan 06, 2011 at 11:00 AM
You could add the following to the Update function in the script attached to your puck: transform.position.y = 0;
Hi,
Thnx fr the reply... but it still moves a bit in y position... Actually I have 2 pucks.. I hit 1 puck with other using power. But in some cases, what happens is puck 1 goes over the puck 2.... ? What to do plz help bro
When you say 'goes over' do you mean it goes up and over or it just goes 'through' the other puck?
You could try Rigidbody.useGravity = false; on each puck. This should get rid of the force being applied to them in the y direction.
did that but no use... Its happening at random. Sometimes the puck travels in y and sometimes it doesnt...plz help
Answer by Kurius · Oct 08, 2017 at 03:06 PM
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.