Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can't use aimConstraint.data.sourceObjects.SetWeight()

Discussion in 'Animation Rigging' started by LightmassGamesDalton, May 4, 2020.

  1. LightmassGamesDalton

    LightmassGamesDalton

    Joined:
    Nov 6, 2017
    Posts:
    4
    Setting the weight doesn't seem to work for Aim Constraint source objects.

    Doesn't work:

    Code (CSharp):
    1. aimConstraint.data.sourceObjects.SetWeight(0, x);
    2. aimConstraint.data.sourceObjects.SetWeight(1, y);
    Does work:

    Code (CSharp):
    1. var a = aimConstraint.data.sourceObjects;
    2. var a0 = a[0];
    3. var a1 = a[1];
    4. a0.weight = x;
    5. a1.weight = y;
    6. a[0] = a0;
    7. a[1] = a1;
    8. aimConstraint.data.sourceObjects = a;
    Maybe I'm doing something wrong but I thought I should report this somewhere.

    Thanks for your time.
     
  2. simonbz

    simonbz

    Unity Technologies

    Joined:
    Sep 28, 2015
    Posts:
    295
    Hi,

    Yes, this is the intended way to set `sourceObjects` in Multi constraints. `sourceObjects` is of type WeightedTransformArray which is a struct, so calling SetWeight on it directly just modifies a copy of the data.

    You could also simplify your working code with the following:

    Code (CSharp):
    1. var a = aimConstraint.data.sourceObjects;
    2. a.SetWeight(0, x);
    3. a.SetWeight(1, y);
    4. aimConstraint.data.sourceObjects = a;
     
  3. LightmassGamesDalton

    LightmassGamesDalton

    Joined:
    Nov 6, 2017
    Posts:
    4
    Hi Simon,

    Thank you for clarifying.

    Cheers
     
  4. LabOSM

    LabOSM

    Joined:
    Sep 5, 2019
    Posts:
    18
    I don't quite understand why you would want to copy the whole struct everytime just to change a value and then copy it back... why can't you just edit it in place? That's what SetWeight() should be for.
     
  5. simonbz

    simonbz

    Unity Technologies

    Joined:
    Sep 28, 2015
    Posts:
    295
    Hi,

    WeightedTransformArray is defined as a struct which is a value type. Therefore, directly calling `SetWeight` on sourceObjects would only modify a copy of it.

    By making WeightedTransformArray a struct, we also allow for the weight values to be animated. This wouldn't have been possible with a class.
     
    unity_LvdTGwgj0IIAqA likes this.
  6. yud_reish

    yud_reish

    Joined:
    Oct 1, 2020
    Posts:
    1
    I had to implement the logic in LateUpdate as the animation over wrote the values that were set in Update. Hope it helps.