As the title suggestions, I am curious as to how one is able to change the source object of the Multi Aim Constraint via script? I can’t seem to reference the source object via script. Am I missing something? Or is there some work around needed?
When Animation Rigging builds its rig, it’s creating a PlayableGraph with Animation C# Jobs that are evaluated by the Animator. However, a job does not refer to a transform directly, but will instead use transform handles (stream handles or scene handles) associated to transforms in the game object hierarchy. These will update when either animated, or modified by scripts, but changing a transform reference in a constraint will not update its handle counterpart.
That is also the reason why constraint like MultiAimConstraint were built with multiple source objects in mind. By manipulating the weights, you can select which source object will drive your constraint and change from one transform to another.
If you really need to instantiate a transform dynamically however, you’ll need to rebuild the Animation Rigging graph for it to update with the new transform references. Something like this should work:
var animator = GetComponent<Animator>();
var rigBuilder = GetComponent<RigBuilder>();
animator.enabled = false;
rigBuilder.Build();
animator.enabled = true;
headWeightTransform = new WeightedTransform(targetIk,headLookWeight);
WeightedTransformArray sources = new WeightedTransformArray();
sources.Add(headWeightTransform);
HeadMultiAimConstraint.data.sourceObjects = sources;
anim.enabled = false;
rigBuilder.Build();
anim.enabled = true;
this will change your whole source Object array, whatever you add object in script will be there , make sure you don’t Build the RigBuilder in every frame… just a tip