Search Unity

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.