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

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

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().

Oh thanks! I haven’t had much to do with playable graphs but that sounds pretty straightforward. :slight_smile:

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!

using Ludiq;
using Portal;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Animations.Rigging;

public class TestRigBuilder : MonoBehaviour
{
    public GameObject SourceObject;
    public GameObject ConstrainedObject;

    public void Awake() {
        Rig rig = (Rig)transform.AddChild<Rig>();
        AnimationRiggingUtility.AddMultiAimConstraint(rig.gameObject, ConstrainedObject.transform, SourceObject.transform, 1f, MultiAimConstraintData.Axis.Y);
        RigBuilder rigbuilder = GetComponent<RigBuilder>();
        rigbuilder.layers.Clear();
        rigbuilder.layers.Add(new RigBuilder.RigLayer(rig, true));       
        rigbuilder.enabled = true;
        rigbuilder.Build();
    }
}

/// <summary>
    /// Add multi aim constraint on target.
    /// </summary>
    /// <param name="target"></param>
    /// <param name="constrainedObject"></param>
    /// <param name="sourceObject"></param>
    /// <param name="sourceObjectWeight"></param>
    /// <param name="aimAxis"></param>
    /// <returns></returns>
    public static Component AddMultiAimConstraint(GameObject target, Transform constrainedObject, Transform sourceObject, float sourceObjectWeight, MultiAimConstraintData.Axis aimAxis) {
        MultiAimConstraint constraint = target.GetComponent<MultiAimConstraint>();
        if (!constraint) {
            constraint = target.AddComponent<MultiAimConstraint>();
        }

        constraint.data.constrainedObject = constrainedObject;
        constraint.data.aimAxis = aimAxis;
        WeightedTransformArray sources = new WeightedTransformArray(0);
        sources.Add(new WeightedTransform(sourceObject, sourceObjectWeight));
        constraint.data.sourceObjects = sources;

        return constraint;
    }
1 Like

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.

After more testing, it seems like some simpler constraints work like the BlendConstraint.

The reason why some constraint wouldn’t work is that you have to call the Reset() function right after creating the constraint. The full explanation can be found here:

1 Like

Oh cool, thanks @danUnity_1 ! I’ll have another try today.