MultiAimConstraint - Changing Source Via Script?

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?

Cheers!

Bump.

Hi,

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;

Thank you for the example on how to rebuild the graph.
Can you give an example on how I could set the new transforms?

1 Like

this is how I was trying but failed:

        multiAimConstraint = GetComponent<MultiAimConstraint>();
        aimPoint = PlayerCamera.playerCamera.GetComponent<PlayerCamera>().aimPoint;

     
        WeightedTransformArray sources = new WeightedTransformArray();
        sources.Add(new WeightedTransform(aimPoint, 1f));
        multiAimConstraint.data.sourceObjects = sources;


        animator.enabled = false;
        rigBuilder.Build();
        animator.enabled = true;

I ended making source aim point on whitch one I referenced through inspector

and I’m making another object dinamically whitch finds that source aim point and changes position of the source aim point

this work around works good for me

I used a workaround. Instead of setting source transform in runtime i just moved existing source object in runtime:

[SerializeField] private Transform _lookAtTarget;
private void Update()
{
_lookAtTarget.position = Player.HeadTransform;
}

or just reparent it so not to use update:
private void OnPlayerVisible()
{
_lookAtTarget.SetParent(Player.HeadTransform);
}

        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 :wink: