Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question MultiAimConstraint - Changing Source Via Script?

Discussion in 'Animation Rigging' started by TragicallyCanadian, Jun 18, 2021.

  1. TragicallyCanadian

    TragicallyCanadian

    Joined:
    Mar 26, 2020
    Posts:
    27
    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!
     
  2. TragicallyCanadian

    TragicallyCanadian

    Joined:
    Mar 26, 2020
    Posts:
    27
  3. simonbz

    simonbz

    Unity Technologies

    Joined:
    Sep 28, 2015
    Posts:
    295
    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:

    Code (CSharp):
    1. var animator = GetComponent<Animator>();
    2. var rigBuilder = GetComponent<RigBuilder>();
    3.  
    4. animator.enabled = false;
    5. rigBuilder.Build();
    6. animator.enabled = true;
    7.  
     
  4. mrphilipjoel

    mrphilipjoel

    Joined:
    Jul 6, 2019
    Posts:
    33
    Thank you for the example on how to rebuild the graph.
    Can you give an example on how I could set the new transforms?
     
    hioctane likes this.
  5. MilitaryG

    MilitaryG

    Joined:
    Apr 26, 2021
    Posts:
    27
    this is how I was trying but failed:

    Code (CSharp):
    1.         multiAimConstraint = GetComponent<MultiAimConstraint>();
    2.         aimPoint = PlayerCamera.playerCamera.GetComponent<PlayerCamera>().aimPoint;
    3.  
    4.      
    5.         WeightedTransformArray sources = new WeightedTransformArray();
    6.         sources.Add(new WeightedTransform(aimPoint, 1f));
    7.         multiAimConstraint.data.sourceObjects = sources;
    8.  
    9.  
    10.         animator.enabled = false;
    11.         rigBuilder.Build();
    12.         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