0
\$\begingroup\$

I have decided to move from Unity's old input system on to the New Input System to control my mobile game.

game preview

I am having some issues with buttons, especially the a and b buttons. The A button represents the W key on the keyboard and the B button represents the Space key on the keyboard, while the joystick is implemented using the new input system using PlayerInput.actions[" "].readValue<TValue>(), but for those two buttons I don't have any idea how to bind them to the keyboard. I have already added OnScreenButton but when I play the game, the buttons not working.

Inspector

private void Update()
{
    this.thrusting = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow);

    if (m_MoveAction.ReadValue<Vector2>().x > 1 || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
    {
        this.turnDirection = 1.0f;
    }
    else if (m_MoveAction.ReadValue<Vector2>().x < -1 || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
    {
        this.turnDirection = -1.0f;
    }
    else
    {
        this.turnDirection = 0.0f;
    }

    if (Input.GetKeyDown(KeyCode.Space))
    {
        Shoot();
    }
}

I have added

this.thrusting = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow);

but why is it not working when I press the A button? What am I missing here?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Because you're not using the new input system.

Everything that begins Input. is the old input system. When you enable the new input system, Unity completely disables the old one, and issues explicit error messages to alert you if you try to use any of the disabled code, like Input.GetKey() / Input.GetKeyDown():

InvalidOperationException: You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings.

(This is why it's important to read your error messages, and include them in your question if you don't understand them)

You seem to be assuming that the job of the OnScreenButton is to take on-screen input and route it as key presses to the old input system so you can read it with legacy code. But the error message tells you that's not the case - you cannot use legacy code with the new input system, full stop.

So, we need to handle our input using the new input system.

  • Inside your InputActions asset, you'll have an ActionMap for your movement controls.

  • Inside this action map, create a new Button-type Action for "Thrust" and "Shoot"

  • Create bindings for these actions to the / w and Space keys.

Example of action configuration

Just like you've done for the joystick action variable m_MoveAction, you can assign actions for the buttons:

m_ThrustAction = playerInput.actions["Thrust"];
m_ShootAction =  playerInput.actions["Shoot"];

Then you can replace

this.thrusting = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow);

with

this.thrusting = m_ThrustAction.ReadValue<float>() > 0;

and

if ( Input.GetKeyDown(KeyCode.Space) )

with

if ( m_ShootAction.triggered )

Or you can use the PlayerInput component to wire-up event callbacks to fire only when the states of these actions change, instead of polling them every frame in update.

\$\endgroup\$
2
  • \$\begingroup\$ I thought I can mix the old one and the new input when I set use both on the Other settings menu, its help me a lot thanks \$\endgroup\$ Oct 20, 2021 at 3:02
  • \$\begingroup\$ Even that wouldn't take input pushed into the new system by OnScreenButton and clone it into the old system. \$\endgroup\$
    – DMGregory
    Oct 20, 2021 at 11:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .