- Home /
How can I constrain physics in all directions except one?
Hey everybody,
Got a question regarding physics in our game. We're making a 2.5D puzzle game in which most of the objects are constrained in both the Z and X directions, so that they can only move up and down. We have gravity affecting these objects so that they will fall as expected.
However, the problem we are having is that you can drag these objects around with a spring joint and sometimes you drag an object upwards when other objects are on top of it. While I want the objects above to still be affected by gravity, I want to disallow upward movement - so that dragging an object upward won't push other objects upward as well, they will just stay in place.
Is there any way to accomplish this without using kinematic rigidbodies? I suppose I want to constrain movement along every axis except for the negative y axis. I've tried clamping the velocity of the object to at most 0; while this works better, you can still push objects upwards little by little over time.
I guess my final question is, if this sort of thing would be best accomplished using kinematic rigidbodies, is it possible to simulate gravity and object collision using these?
Thanks!
Answer by kromenak · Jan 25, 2012 at 08:20 PM
Ultimately, I've arrived at the conclusion that you can't, or at least it isn't exposed in Unity. While you can attempt to apply an equal downward force, this proved to be buggy and easy enough to glitch by applying force for a long enough period of time.
Our solution was ultimately to abandon using physics for this constrained movement and program transform movement that mimics the needed behavior.
Answer by testure · Jun 13, 2011 at 05:18 PM
Not sure which version of unity you're using but in 3.3 they added the ability to contrain individual axis position and rotation on rigid bodies. Should be there in the rigid body inspector, and you can access it via script as well.
Hey, we're using this feature to constrain movement in the Z and X directions and also to constrain rotation in the Y and X directions. However, the problem we are having is that we only want our objects to move downward - so, constraining the movement along one axis in one direction.
Answer by slayzar · Feb 17, 2012 at 02:21 AM
Hey everyone. I'm having severe problems trying to get the constraints to work. I am trying this in Update():
transform.rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
But I get this error: The name `RigidbodyConstraints' does not exist in the current context.
Any help would be greatly appreciated.