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

Setting the weight doesn’t seem to work for Aim Constraint source objects.

Doesn’t work:

aimConstraint.data.sourceObjects.SetWeight(0, x);
aimConstraint.data.sourceObjects.SetWeight(1, y);

Does work:

var a = aimConstraint.data.sourceObjects;
var a0 = a[0];
var a1 = a[1];
a0.weight = x;
a1.weight = y;
a[0] = a0;
a[1] = a1;
aimConstraint.data.sourceObjects = a;

Maybe I’m doing something wrong but I thought I should report this somewhere.

Thanks for your time.

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:

var a = aimConstraint.data.sourceObjects;
a.SetWeight(0, x);
a.SetWeight(1, y);
aimConstraint.data.sourceObjects = a;
2 Likes

Hi Simon,

Thank you for clarifying.

Cheers

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.

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.

1 Like

I had to implement the logic in LateUpdate as the animation over wrote the values that were set in Update. Hope it helps.