1

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; 
}
KiynL
  • 4,097
  • 2
  • 16
  • 34

1 Answers1

1

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.

KiynL
  • 4,097
  • 2
  • 16
  • 34
  • 1
    what do these mean Func getter , Action setter? – Draken Forge37 May 26 '22 at 20:04
  • @DrakenForge37 They act like a lambda, with Action you can set your command based on the moving float. – KiynL May 26 '22 at 20:11
  • 1
    Could I get an example please. I cut and paste the code,but im getting an error with Func getter and Action setter – Draken Forge37 May 26 '22 at 20:35
  • ops.. you need `using System` ; – KiynL May 26 '22 at 20:36
  • Sorry, I have no idea what happened. This code gives you a smooth float motion. If there is no problem in this regard, you have taken a step forward. If the problem is with the pebbles and IKs, you need to improve the code. The question you asked will be resolved with Tweener. – KiynL May 26 '22 at 21:23
  • looks like it has to do with my recoil animation . might be because its on the override layer. – Draken Forge37 May 26 '22 at 21:36