Search Unity

Poor Man's Rigidbody Constraints (Unity 2.6)

Discussion in 'Physics' started by kr4ft3r, Feb 17, 2015.

  1. kr4ft3r

    kr4ft3r

    Joined:
    Feb 18, 2014
    Posts:
    10
    Hello. I would like to know if there is a way to use constraints functionality for older (free license) versions of Unity which don't seem to support .constraints property of rigidbody. What I am trying to achieve is a rigidbody (which is subject to forces and attached by a spring joint to another body) to be rotateable on only one axis and rotation frozen on other two, like what freezeRotation does for all 3 axis but with one being free to rotate around.
    Any outside libs or ideas how to solve this? It is a very active rigidbody and often updated in FixedUpdate so I don't think simple resetting two axis of rotation will do the trick, preferably it should work almost as smooth as freezeRotation without jerkiness.
    Thanks.
     
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    Yup! You can use a ConfigurableJoint and its constraints. Depending on how active the joint is you might want to tweak the Projection Mode settings; this kind of joint-based constraint will be a little spongier/softer than the built-in constraints. You can either configure this purely in the Inspector (with no connected body the Rigidbody is pinned to the world), or via code with:

    Code (csharp):
    1.  
    2. /**
    3. * Create a configurablejoint with everything locked
    4. */
    5. static function Create2DJoint(from:Rigidbody, to:Rigidbody, at:Vector3):ConfigurableJoint
    6. {
    7.     var joint:ConfigurableJoint = from.gameObject.AddComponent(ConfigurableJoint);
    8.      
    9.     joint.connectedBody = to;
    10.     joint.anchor = from.transform.InverseTransformPoint(at);
    11.      
    12.     joint.xMotion = ConfigurableJointMotion.Free;
    13.     joint.yMotion = ConfigurableJointMotion.Free;
    14.     joint.zMotion = ConfigurableJointMotion.Locked;
    15.      
    16.     joint.angularXMotion = ConfigurableJointMotion.Locked;
    17.     joint.angularYMotion = ConfigurableJointMotion.Locked;
    18.     joint.angularZMotion = ConfigurableJointMotion.Free;
    19.      
    20.     joint.projectionMode = JointProjectionMode.PositionOnly;
    21.      
    22.     joint.configuredInWorldSpace = true;
    23.  
    24.     return joint;
    25. }
    26.  
    That's some JavaScript (from a Unity 2.x project!), but should be easy to change over to C#.