Issue with procedural animation

Hi, in Unity, I am using the following code to generate animations during runtime. However, I am having an issue within the arm scaling segments of the code. Whenever a new animation is generated and played, the hand positions move flawlessly, but the armsegmentlength variable within the IK script remains unchanged. The animation generates, the keyframes are applied, and the variables change, but it is not applied to the IK script.

I have pinpointed this issue down to the animation not being able to locate the “RightArm” and “LeftArm” IK scripts. Which is strange, as it can detect the arm objects when moving the hands. Attached are screenshots of my hierarchy, an example animation that is generated, and the code I use to make the animations.

If you could yield some information I may of missed, I would be most grateful.


image

  public void prepareMakeAnimationClip(Vector3 L_RelativePos, Vector3 R_RelativePos, float L_Length, float R_Length, float timeToTransfer)
    {
        if (handAnimator.GetCurrentAnimatorStateInfo(0).IsName("CustomHandPosSlot1"))
        {
            //Use Slot 2
            makeAnimationClip(L_RelativePos, R_RelativePos, L_Length, R_Length, timeToTransfer, "CustomHandPosSlot2", theDynamicHandClipSlot2);
        }
        else
        {
            makeAnimationClip(L_RelativePos, R_RelativePos, L_Length, R_Length, timeToTransfer, "CustomHandPosSlot1", theDynamicHandClipSlot1);
        }
    }

    public void makeAnimationClip(Vector3 L_RelativePos, Vector3 R_RelativePos, float L_Length, float R_Length, float timeToTransfer, string animationToOverride, AnimationClip clipToOverride)
    {
        AnimatorOverrideController AOC = new AnimatorOverrideController(handAnimator.runtimeAnimatorController);
        AnimationClip clip = new AnimationClip();
        clip.legacy = false;

        Keyframe[] leftPositionXKeys = new Keyframe[2];
        leftPositionXKeys[0] = new Keyframe(0f, leftArm.HandPoint.localPosition.x);
        leftPositionXKeys[1] = new Keyframe(timeToTransfer, L_RelativePos.x);

        AnimationCurve leftPositionXCurve = new AnimationCurve(leftPositionXKeys);
        clip.SetCurve("Left Arm/Hand", typeof(Transform), "localPosition.x", leftPositionXCurve);



        Keyframe[] leftPositionYKeys = new Keyframe[2];
        leftPositionYKeys[0] = new Keyframe(0f, leftArm.HandPoint.localPosition.y);
        leftPositionYKeys[1] = new Keyframe(timeToTransfer, L_RelativePos.y);

        AnimationCurve leftPositionYCurve = new AnimationCurve(leftPositionYKeys);
        clip.SetCurve("Left Arm/Hand", typeof(Transform), "localPosition.y", leftPositionYCurve);



        Keyframe[] rightPositionXKeys = new Keyframe[2];
        rightPositionXKeys[0] = new Keyframe(0f, rightArm.HandPoint.localPosition.x);
        rightPositionXKeys[1] = new Keyframe(timeToTransfer, R_RelativePos.x);

        AnimationCurve rightPositionXCurve = new AnimationCurve(rightPositionXKeys);
        clip.SetCurve("Right Arm/Hand", typeof(Transform), "localPosition.x", rightPositionXCurve);



        Keyframe[] rightPositionYKeys = new Keyframe[2];
        rightPositionYKeys[0] = new Keyframe(0f, rightArm.HandPoint.localPosition.y);
        rightPositionYKeys[1] = new Keyframe(timeToTransfer, R_RelativePos.y);

        AnimationCurve rightPositionYCurve = new AnimationCurve(rightPositionYKeys);
        clip.SetCurve("Right Arm/Hand", typeof(Transform), "localPosition.y", rightPositionYCurve);



        Keyframe[] rightScaleKeys = new Keyframe[2];
        rightScaleKeys[0] = new Keyframe(0, rightArm.armSegmentLength);
        rightScaleKeys[1] = new Keyframe(timeToTransfer, R_Length);
        AnimationCurve rightScaleCurve = new AnimationCurve(rightScaleKeys);
        clip.SetCurve("Right Arm", typeof(IK), "ArmSegmentLength", rightScaleCurve);

        Keyframe[] leftScaleKeys = new Keyframe[2];
        leftScaleKeys[0] = new Keyframe(0, leftArm.armSegmentLength);
        leftScaleKeys[1] = new Keyframe(timeToTransfer, L_Length);
        AnimationCurve leftScaleCurve = new AnimationCurve(leftScaleKeys);
        clip.SetCurve("Left Arm", typeof(IK), "ArmSegmentLength", leftScaleCurve);

        clipToOverride = clip;
        AOC[animationToOverride] = clip;
        handAnimator.runtimeAnimatorController = AOC;

        handAnimator.Play(animationToOverride);
    }

I have found a workaround that has fixed the issue. I figured that since that the animation has no trouble accessing transform variables (Position, rotation, scale), all I had to do was just set the armSegmentLength variable to a value of a child transform object, and I could just change that. It would be more efficient if I could directly change the value of armSegmentLength, but this has appeared to fix the issue.

         Keyframe[] rightScaleKeys = new Keyframe[2];
        rightScaleKeys[0] = new Keyframe(0, rightArm.segmentLengthDefiner.localScale.x);
        rightScaleKeys[1] = new Keyframe(timeToTransfer, R_Length);
        AnimationCurve rightScaleCurve = new AnimationCurve(rightScaleKeys);
        clip.SetCurve("Right Arm/Arm Segment Length Definer", typeof(Transform), "localScale.x", rightScaleCurve); //I wrote some code in another script that set armSegmentLength to the localScale.x variable of Arm Segment Length Definer