Grekys
Grekys

Reputation: 41

What is the Unity Input System equivalent of GetAxis?

I'm trying to get input from my character using the Input System package when I press the left/right arrows keys, however when I read the value of the input it currently snaps between 0, -1, and 1. This is similar to the Input Manager's GetAxisRaw, however, what I want is to be able to do something more like GetAxis where it's not instantly 1, but goes through a couple float numbers between 0 and 1 up until 1. Same when it goes back down as well.

Preferably, I'd like to know if the current Input System has a way to manage that on its own, rather than needing to adjust the input myself through any mathematical formulas over time.

What I'm trying to achieve is smooth movement left and right, where for a split second or so the character picks up speed before they reach the max, and where letting go they also still slide for a split second before stopping. Since what I'm doing is setting the velocity equal to the multiplied value of the speed (rigidbody.velocity = new Vector2(//input value * movement speed, rigidbody.velocity.y)).

Of course I understand that if there is no way to create a sort of "ramping" effect to the snapping numbers, I could make it an acceleration speed instead (rigidbody.velocity += new Vector2(blah blah)). The issue being I then need to insert extra code to set a cap for the speed, and to slow down the velocity when I let go so my character doesn't slide like they're on ice.

Upvotes: 1

Views: 3147

Answers (1)

Grekys
Grekys

Reputation: 41

No, there doesn't seem to be an equivalent to GetAxis for the Unity Input System package, however I did attempt to emulate it. The Unity Docs note that Pasted from Unity Docs "the Horizontal and Vertical ranges change from 0 to +1 or -1 with increase/decrease in 0.05f steps."

So I used Mathf.Lerp(a, b, t) (https://docs.unity3d.com/ScriptReference/Mathf.Lerp.html) where my starting value (a) was 0, and my ending value (b) was the input (whether it be 1 or -1). My interpolation value (t) starts at 0 and I would add 0.05 to it each frame. This was a bit too slow, so I multiplied it by an int that I called inputSensitivity.

For the visual learners:

[SerializeField] private float moveSpeed = 5f;

    private InputMaster inputActions;
    private Rigidbody2D rigidbody2D;

    static float movementIncrementOverTime = 0;
    [SerializeField] int inputSensitivity = 100;

    private void Awake()
    {
        inputActions = new InputMaster();
    }

    private void OnEnable()
    {
        inputActions.Enable();
    }

    private void OnDisable()
    {
        inputActions.Disable();
    }

    private void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        //Read the movement value

        float moveInput = Running();
        inputActions.Player.Movement.canceled += Movement_canceled;

        //Move the player
        rigidbody2D.velocity = new Vector2(moveInput * moveSpeed, rigidbody2D.velocity.y);
    }

    private float Running()
    {
        float moveInput = inputActions.Player.Movement.ReadValue<float>();
        switch(moveInput)
        {
            case 1:
                moveInput = Mathf.Lerp(0, moveInput, movementIncrementOverTime);
                movementIncrementOverTime += (0.05f * inputSensitivity) * Time.deltaTime;
                break;

            case -1:
                moveInput = Mathf.Lerp(0, moveInput, movementIncrementOverTime);
                movementIncrementOverTime += (0.05f * inputSensitivity) * Time.deltaTime;
                break;

            default:
                break;
        }

        return moveInput;
    }

    private void Movement_canceled(InputAction.CallbackContext obj)
    {
        movementIncrementOverTime = 0;
    }

The Running() function is the key here, it makes it so that instead of snapping to 1 and -1, it gradually moves up to those values. Then, in the Update() function the line of code inputActions.Player.Movement.canceled += Movement_canceled; checks for when I release the key and resets my static float movementIncrementOverTime to 0 so I can once again ramp up speed when I decided to move in the opposite direction, or if I stand still and start walking again.

The only issue, which I have yet to fix, is that I haven't figured out how to get the character to slide slightly when you let go of the key, instead of instantly stopping when you let go.

Upvotes: 0

Related Questions