Change hand IK over time instead of it just snapping to 0 and 1

Viewed 65

I'm trying to get my characters rig to go from hand IK 0 to 1 over a period of time instead of just snapping to it. How could I go about doing that?

public TwoBoneIKConstraint rightHandIK;  //on animator manager script // getting hand IK from inspector so can change weight
public TwoBoneIKConstraint leftHandIK;   //on animator manager script

    if (animatorManager.isAimingGunn)
{
    animatorManager.leftHandIK.weight = 1; 
    animatorManager.rightHandIK.weight = 1; 
    horizontalMovementInput = 0f;
    verticalMovementInput = 0f;
}
else if(animatorManager.isAimingGunn == false)
{
    animatorManager.rightHandIK.weight = 0;
    animatorManager.leftHandIK.weight = 0; 
}
1 Answers

For this purpose you need to run a Tweener. This is a basic float Tweener that smoothly changes the floats by running Coroutine.

public IEnumerator DoTween(Func<float> getter , Action<float> setter, float targetValue, float duringTime = 1f)
{
    var progress = 0f;

    var init = getter();
    
    while (progress < 1)
    {
        progress += Time.deltaTime/duringTime;

        setter.Invoke(Mathf.Lerp(init, targetValue, progress));

        yield return new WaitForEndOfFrame();
    }
}

Do this to run Tweener in if code, Also, to zero the weight only, set zero in the targetValue parameter:

// How to run tweener?
// for E.G set IK weight to 1 during 2 seconds

StartCoroutine(DoTween(
    () => animatorManager.leftHandIK.weight, 
    x => animatorManager.leftHandIK.weight = x,
    1f, 2f
));

Also, if you want to create similar effects in the code, it suggests using the great DoTween unity plug-in.

Related