Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent of GetKey in the new unity input system?

Tags:

c#

unity3d

There's Started, Performed and Cancelled, but what if I want to detect if I'm holding down the button on a Gamepad. So basically, how do i rewrite this to a gamepad with the new input system.

private void Jump()
    {
        if (isTouchingGround == true && Input.GetKeyDown(KeyCode.Space))
        {
            isJumping = true;
            jumpTimeCounter = jumpTime;
            myRigidbody2D.velocity = new Vector2(myRigidbody2D.velocity.x, 1 * jumpSpeed);
        }

        if (Input.GetKey(KeyCode.Space) && isJumping == true)
        {
            if (jumpTimeCounter > 0)
            {
                myRigidbody2D.velocity = new Vector2(myRigidbody2D.velocity.x, 1 * jumpSpeed);
                jumpTimeCounter -= Time.deltaTime;
            }
            else
            {
                isJumping = false;
            }
        }
        if (Input.GetKeyUp(KeyCode.Space))
        {
            isJumping = false;
        }
    }
like image 504
jaskolajban Avatar asked Mar 09 '20 08:03

jaskolajban


People also ask

What is input Getkey in Unity?

Description. Returns true while the user holds down the key identified by name . GetKey will report the status of the named key. This might be used to confirm a key is used for auto fire. For the list of key identifiers see Input Manager.

What is the new input system in Unity?

In Unity's new Input System, Actions connect the physical inputs of a control device with something that happens in the game. It's how you make certain buttons, do certain things. Examples include moving the player, jumping, firing a gun, etc. Basically, anything that the person playing the game can do in the game.

Is the new Unity input system good?

it makes cross-platform controls easy: compared to the old input system, it is way faster to perform the same action via your keyboard, an Xbox controller, a PlayStation gamepad, etc. and keep all devices consistent and in sync.


2 Answers

There is no equivalent! You have to Bind all Inputs to events and then process the inputs in the methods you assign to these events. This is because the new input system does not directly use Keys to make it work on multiple devices (keyboard, controller, phone, ...) without adding more code

See here.

like image 172
etaxi341 Avatar answered Nov 10 '22 02:11

etaxi341


The new input system exposes a Gamepad object with buttons each button is a ButtonControl that exposes accessors for the button state:

Input.GetKeyDown(KeyCode.JoystickButton2) ~= Gamepad.current.buttonWest.wasPressedThisFrame

Input.GetKey(KeyCode.JoystickButton2)     ~= Gamepad.current.buttonWest.isPressed

Input.GetKeyUp(KeyCode.JoystickButton2)   ~= Gamepad.current.buttonWest.wasReleasedThisFrame

Or if you want keyboard use Keyboard.

That lets you circumvent the new input system's configuration and hardcode your control scheme.


However, for supporting multiple input devices, you'd create a control scheme and use accessors on it:

Input.GetButton("fire")     ~= m_Controls.fire.ReadValue<bool>()

Input.GetButtonUp("fire")   ~= m_Controls.fire.triggered
Input.GetButtonDown("fire") ~= m_Controls.fire.triggered

triggered doesn't work quite like Input.GetButtonUp/Down. You'd configure the "fire" input for when during a press it should fire. You can set an input in your control scheme to be a Tap or SlowTap (ugh) and if it's a Tap, then triggered happens if it was released within the threshold time whereas SlowTaps are triggered if it's held for the threshold time.

You can also check which phase of press the input is in:

m_Controls.fire.phase == InputActionPhase.Started

If you go for this control scheme setup, see also this post about bad defaults.

I'd rewrite your code something like this (untested) and configure "jump" as a SlowTap with immediate activation:

void Jump()
{
    // maybe need to clear isJumping if isTouchingGround?
    if (isJumping)
    {
        if (m_Controls.gameplay.jump.ReadValue<bool>() && jumpTimeCounter > 0)
        {
            myRigidbody2D.velocity = new Vector2(myRigidbody2D.velocity.x, 1 * jumpSpeed);
            jumpTimeCounter -= Time.deltaTime;
        }
        else
        {
            isJumping = false;
        }
    }
    // Use triggered to avoid jumping again when we hold button on landing
    else if (isTouchingGround && m_Controls.gameplay.jump.triggered)
    {
        isJumping = true;
        jumpTimeCounter = jumpTime;
        myRigidbody2D.velocity = new Vector2(myRigidbody2D.velocity.x, 1 * jumpSpeed);
    }
}

(But I'm not particularly advanced with the new input system.)

I'd recommend playing around with the simple demo sample that can be imported from the package window. See how the different ways of using the system work.

like image 44
idbrii Avatar answered Nov 10 '22 01:11

idbrii