Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Resolved Multi-position Constraint can't change source objects weight via code

Discussion in 'Animation Rigging' started by skelitheprogrammer, Jan 8, 2022.

  1. skelitheprogrammer

    skelitheprogrammer

    Joined:
    Sep 20, 2019
    Posts:
    15
    Hello. Im making foot ik system which allow feet stick to the hit normal.

    For better work im started using multi position constraint with two-bone ik constraint.

    The problem is that i can't change weight in multi position source objects via code when I call SetWeight(int index, float weight);

    Why? And how to change the weight of source objects in multi-position constraint script?
     
  2. skelitheprogrammer

    skelitheprogrammer

    Joined:
    Sep 20, 2019
    Posts:
    15
    I ended up creating this method to make things working

    Code (CSharp):
    1.     private void SetMultiPositionSourceWeight(float weight, params int[] indexes)
    2.     {
    3.         var sources = _legPositionIK.data.sourceObjects;
    4.  
    5.         foreach (var item in indexes)
    6.         {
    7.             sources.SetWeight(item, weight);
    8.         }
    9.  
    10.         _legPositionIK.data.sourceObjects = sources;
    11.     }
    _legPositionIK = Multi-Position Constraint
     
  3. simonbz

    simonbz

    Unity Technologies

    Joined:
    Sep 28, 2015
    Posts:
    295
    That seems about right. You need to copy the `sources` array back in `sourceObjects` whenever you update weights. `sourceObjects` is not a reference, it's a struct object, and therefore it's a copy of the original data.

    Using a struct makes it possible for the weights to be animated, but it's a bit more finicky if you want to update them manually at runtime.
     
  4. skelitheprogrammer

    skelitheprogrammer

    Joined:
    Sep 20, 2019
    Posts:
    15
    Yea, I'm just trying to place my legs correctly and found that a multi-position constraint is useful when you want to align the position of your legs and make sure they don't fly in the air. But I'm still can't make it work perfectly.