Animation Rigging IK rigs work in Editor Play Mode, but break in a built executable

I have a setup modeled after TheKiwiCoder’s youtube tutorials, where there’s an animator animating the character’s idle-walk-run blendtree, and other animations (opening the palms for example), and the animation rigs have a parent object (“AnimationRigs”) which has a separate animator animating the different rig weights. For example animating the arm IK’s after the targets are positioned on a box to grab, or animating only the right arm IK rig to press a button.

setup:

This works for me in the editor: https://www.youtube.com/watch?v=KxmHXevUrlg

But when I try doing a build executable it looks like the IK’s are offset: https://www.youtube.com/watch?v=-MzYytrCtws

The problem only seems to be with the Two Bone IK Constrained. I have several Multi Aim Constraints setup for the head aiming at the various gameobjects in the scene which works correctly in both versions.

There’s also a MultiParent constraint for a Helper object I use to parent the carried boxes. And so the hands stick to the boxes the IK target are parented to that object.

I tried building and running with default unity sphere objects attached to the IK targets to verify they are positioned correctly and they are, it’s just the IK is following them with an offset in the built version.

I had a similar problem in the editor when I moved the IK target objects in the editor, when hitting play they would maintain offset. But in the videos above they are aligned to the Hand_R and Hand_L transforms in the t pose in the editor and in the editor it works correctly.

I found a post from last year with a similar problem, where the IK wouldn’t work at all and it turned out something was tagged as EditorOnly (Animation Rigging behavior different in built player and editor). But in my case I didn’t find anything tagged like that in my hierarchy and the IK rigging seems to work, it’s just offset.

UPDATE:

I added code in one of my character’s components Start():

        var rig = GetComponent<RigBuilder>();
        rig.Build();
        foreach (var a in GetComponentsInChildren<Animator>()) {
            a.Rebind();
        }

and the IK is was broken in the Editor just like in the built version! So because it was previously related to the starting position of the targets, I also aligned the IK targets with their source transforms they’re supposed to animate:

        var rigBuilder = GetComponentInParent<RigBuilder>();
        foreach(var mrc in GetComponentsInChildren<MultiRotationConstraint>()) {
            var target = mrc.data.sourceObjects[0].transform;
            var source = mrc.data.constrainedObject;
            target.position = source.position;
            target.rotation = source.rotation;
        }
        rigBuilder.Build();
        foreach(var animator in GetComponentsInChildren<Animator>()) {
            animator.Rebind();
        }

(I think in my case the mismatch is cause by another component which is using SampleAnimation - which was repositioning the bones? With some timing issue causing it to break in build but still work in the Unity Editor)

And now everything works in Editor and in a standalone build!
Maybe this helps someone else, and maybe the TwoBoneIKConstraint should always try to aim for the target ignoring any offset at the time of setup?