Search Unity

[Solved] Manipulating Joint Limits at Runtime

Discussion in 'Scripting' started by Tinus, Mar 2, 2010.

  1. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    Hey all!


    I'm working on a simple skeleton/muscle system using rigidbodies and ConfigurableJoints.

    Consider the following (totally epic) diagram:



    If you keep your knee stretched, you can only move your leg up until you reach a certain limit(unless you are super agile, in which case: go you!). To continue past this limit (pulling your leg up against your chest) your knee has to bend.

    I wanted to model this by modifying the knee-joint's SoftJointLimit property at runtime. As soon as the upper leg is at a certain height, the knee's limits will start to get smaller so that the lower leg is forced back down.

    The problem is... The joint limits seem to be immutable once the game is running! The script will not compile if I try to change SoftJointLimit.limit directly, and cheekily attempting to replace the SoftJointLimit by a newly created instance just does nothing.

    Code (csharp):
    1. /* C# */
    2.  
    3. // Attempt 1:
    4. m_joint.highAngularXLimit.limit = -20f;
    5.  
    6. // Attempt 2:
    7. SoftJointLimit newLimit = new SoftJointLimit();
    8. newLimit.limit = -20f;
    9. m_joint.highAngularXLimit = newLimit;
    10.  
    So, am I going at this the wrong way? Or is this not possible? If the latter is the case, can you think of a different approach to modeling such context dependent constraints?

    Cheers :)
     
  2. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    I'm a tool.

    The second method I tried actually does work. :eek: