Converting analog stick Vector2 into a float value for animation speed.

Hello everyone! I have recently finished a udemy tutorial on state machines and am working on making changes and improvements to my project now. In the tutorial, the animation speed was set to either 0 or 1, and used a blend tree of just an idle and run animation. However I would like to add a walking animation in between so I’m trying to calculate the value of the thumbstick being pushed in any direction and have it send out a value between 0.6 (the slowest the animation looks good at) and 1. I have come up with something that ALMOST works, however pressing the stick all the way southeast or northwest does not deliver a 1 as it does when i push the rest of the directions. The original code was as follows :

        Vector3 movement = CalculateMovement();

        Move(movement * stateMachine.FreeLookMovementSpeed, deltaTime);

        if (stateMachine.InputReader.MovementValue == Vector2.zero)
        {
            stateMachine.Animator.SetFloat(FreeLookSpeedHash, 0, AnimatorDampTime, deltaTime);
            return;
        }

        stateMachine.Animator.SetFloat(FreeLookSpeedHash, 1, AnimatorDampTime, deltaTime);
        FaceMovementDirection(movement, deltaTime);
    }

and I added a float to try and capture the stick value :

        Vector3 movement = CalculateMovement();

        Move(movement * stateMachine.FreeLookMovementSpeed, deltaTime);

        if (stateMachine.InputReader.MovementValue == Vector2.zero)
        {
            stateMachine.Animator.SetFloat(FreeLookSpeedHash, 0, AnimatorDampTime, deltaTime);
            return;
        }

        float value = Mathf.Abs((stateMachine.InputReader.MovementValue.x) + stateMachine.InputReader.MovementValue.y);
        value = Mathf.Clamp(value, 0.6f, 1f);
        Debug.Log(value);

        stateMachine.Animator.SetFloat(FreeLookSpeedHash, value, AnimatorDampTime, deltaTime);
        FaceMovementDirection(movement, deltaTime);
    }

and the calculateMovement Vector3 I have left unchanged

    private Vector3 CalculateMovement()
    {
        Vector3 forward = stateMachine.MainCameraTransform.forward;
        Vector3 right = stateMachine.MainCameraTransform.right;

        forward.y = 0f;
        right.y = 0f;

        forward.Normalize();
        right.Normalize();
        return forward * stateMachine.InputReader.MovementValue.y +
            right * stateMachine.InputReader.MovementValue.x;
    }

I think thats all the relevant info but I may have missed something, let me know if I need to add more information. I am a bit over my head with this project but I’m learning a lot from it and would like to keep going with it if possible. Any help would be appreciated, and am open to doing things a different way too if anyone has any suggestions. Thank you!

Use MovementValue.magnitude

2 Likes

Doh… As usual I’m over complicating things again… Thank you so much, accomplished exactly what I was after in a much simpler way than I was trying to do, thank you!!

2 Likes