Search code examples
animationunity-game-engineanimator

No jumping of values on GetAxis for smoother animation UNITY3D


I have an issue that I'm wondering if there's a simpler way to solve.

I am using Input.GetAxis(), for example, let's say Horizontal.

Holding D-> with Input.GetAxis("Horizontal") will result in the value gradually going from 0 to 1. Releasing D-> will gradually bring it back down to 0.

Here's my problem. If I am to hold down D->, then press A<- and release D->, the value instead of gradually dropping 1 to 0, then 0 to -1, instead instantly snaps to 0 and proceeds to gradually drop to -1.

I am using this value to control my animations, and it makes them snap all ugly. Is there a way I make the value drop gradually like I intend without much code or am I missing some settings for the animator?


Solution

  •     float tempADFloat = playerAnim.GetFloat("ADInput");
    
        if (_ADInput != 0)
        {
            playerAnim.SetFloat("ADInput", Mathf.Clamp(tempADFloat + 0.1f * _ADInput, -1f, 1f));
    
        } else
        {
            if ((tempADFloat < 0.2 && tempADFloat > -0.2))
            {
                playerAnim.SetFloat("ADInput", 0);
    
            } else if (tempADFloat < 0)
            {
                playerAnim.SetFloat("ADInput", tempADFloat + 0.1f);
    
            } else if (tempADFloat > 0)
            {
                playerAnim.SetFloat("ADInput", tempADFloat - 0.1f);
            }
        }
    

    playerAnim is my characters animator reference. This way animations transition smoothly.

    I use tempADFloat < 0.2 instead of tempADFloat < 0.1 because the value can't be set and checked in time. It will basically end up jumping 0.1 to 0 back and forth