Search Unity

Question Is there a way to add a Rig to the Rig Builder at runtime?

Discussion in 'Animation Rigging' started by petey, May 29, 2020.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    Hi there,

    I've had a little trouble adding rigs to the Rig Builder at runtime.
    I have a script that is setting everything up but is there something I should be adding to kickstart the rigs after this? They just seem completely inactive.

    Thanks!
    Pete
     
  2. simonbz

    simonbz

    Unity Technologies

    Joined:
    Sep 28, 2015
    Posts:
    295
    Hi,
    The RigBuilder assembles the Rigs in a PlayableGraph when RigBuilder is first enabled. If you want to modify the list of rigs at runtime, you would need to also rebuild that PlayableGraph by calling RigBuilder.Build().
     
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    Oh thanks! I haven’t had much to do with playable graphs but that sounds pretty straightforward. :)
     
  4. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    What about building a rig and it's constraints in code and then assigning it to the RigBuilder? I tried rebuilding the RigBuilder at the end but it does not work. Not sure what I'm missing!

    Code (CSharp):
    1. using Ludiq;
    2. using Portal;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.Animations;
    7. using UnityEngine.Animations.Rigging;
    8.  
    9. public class TestRigBuilder : MonoBehaviour
    10. {
    11.     public GameObject SourceObject;
    12.     public GameObject ConstrainedObject;
    13.  
    14.     public void Awake() {
    15.         Rig rig = (Rig)transform.AddChild<Rig>();
    16.         AnimationRiggingUtility.AddMultiAimConstraint(rig.gameObject, ConstrainedObject.transform, SourceObject.transform, 1f, MultiAimConstraintData.Axis.Y);
    17.         RigBuilder rigbuilder = GetComponent<RigBuilder>();
    18.         rigbuilder.layers.Clear();
    19.         rigbuilder.layers.Add(new RigBuilder.RigLayer(rig, true));      
    20.         rigbuilder.enabled = true;
    21.         rigbuilder.Build();
    22.     }
    23. }
    24.  
    25. /// <summary>
    26.     /// Add multi aim constraint on target.
    27.     /// </summary>
    28.     /// <param name="target"></param>
    29.     /// <param name="constrainedObject"></param>
    30.     /// <param name="sourceObject"></param>
    31.     /// <param name="sourceObjectWeight"></param>
    32.     /// <param name="aimAxis"></param>
    33.     /// <returns></returns>
    34.     public static Component AddMultiAimConstraint(GameObject target, Transform constrainedObject, Transform sourceObject, float sourceObjectWeight, MultiAimConstraintData.Axis aimAxis) {
    35.         MultiAimConstraint constraint = target.GetComponent<MultiAimConstraint>();
    36.         if (!constraint) {
    37.             constraint = target.AddComponent<MultiAimConstraint>();
    38.         }
    39.  
    40.         constraint.data.constrainedObject = constrainedObject;
    41.         constraint.data.aimAxis = aimAxis;
    42.         WeightedTransformArray sources = new WeightedTransformArray(0);
    43.         sources.Add(new WeightedTransform(sourceObject, sourceObjectWeight));
    44.         constraint.data.sourceObjects = sources;
    45.  
    46.         return constraint;
    47.     }
    48.  
     
    petey likes this.
  5. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    Actually finally getting a chance to try this too and it doesn't work for me either. I'm on 0.2.6.
    All of my characters are set up at runtime so I'd love to know what's going wrong here.
     
  6. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    After more testing, it seems like some simpler constraints work like the BlendConstraint.
     
  7. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    petey likes this.
  8. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    Oh cool, thanks @danUnity ! I'll have another try today.