Interpolating a dynamic value

Hello! I’m having a problem with interpolating a value that constantly changes - pelvis rotation on x axis for procedural animation. When the character goes up I want them to bend forward and when descending they should bend backward a little. The character now knows when going up or down and somehow choppily bends but I just CAN’T make the movement smooth! I’m no programmer and it’s probably something absolutely stupid. I’ll be super grateful for any help :frowning:

Here’s the code (clean, without all the interpolation stuff I tried)

private void Start()
    {
        anim = GetComponent<Animator>();
        InvokeRepeating("PrevPos", 0, 0.2f);
    }

called from OnAnimatorIK:

        newPelvisRotation = anim.bodyRotation.eulerAngles + whyunotwork;
        anim.bodyRotation = Quaternion.Euler(newPelvisRotation); //<MAKE THIS SMOOTH
    }

    void PrevPos()
    {
        previousPositionY = transform.position.y;
        Invoke("CheckPos", 0.2f);
    }
    void CheckPos()
    {
        RaycastHit hitRay;
        Physics.Raycast(transform.position, Vector3.down, out hitRay, 2f);
        groundSlopeAngle = Vector3.Angle(hitRay.normal, Vector3.up);
        if (transform.position.y < previousPositionY)
        {
            groundSlopeAngle = -groundSlopeAngle;
        }
        whyunotwork = Vector3.right * groundSlopeAngle;
    }

You can rotate a Quaternion to a target over time with RotateTowards: Unity - Scripting API: Quaternion.RotateTowards

When interpolating dynamic value, constantly increate weight over time. Imagine we have a sequnce of X positions recorded as animation keyframes with .5 second step:

0 1 -1 2 -2 1 -1 0

This sequnce will move object around 0 with increasing amplitude , then the aplitude wil decrease and object will stop back at 0. Now imagine we want to interpolate it’s position to 1 during that animation.

For that we do:

private void LateUpdate() {
  x = Mathf.Lerp(x, 1, timeSinceStart / duration);
}

With this, object will start waving around 0 and stop at 1 smoothly and preserving original animated move.

Thank you for your reply! Yes, I’ve tried that but that was one of the things that didn’t work. I’m probably using it the wrong way otl

Thank you so much, I’ll try this!