Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

New Input System : Pressing secondary arrow button while holding another

Discussion in 'Input System' started by sophoni, Jul 18, 2022.

  1. sophoni

    sophoni

    Joined:
    Jul 19, 2015
    Posts:
    5
    Hey folks,

    I'm trying to change the input system on my game to use the new input system.

    I'm trying to replicate the following process:

    Receive key inputs (arrows) for up / down / left / right movement and while holding the left arrow for example, if another key is pressed let's say up, I want to allow my player controller to receive the new direction (while still potentially holding the left button) so I would like the player to move in the up direction (not both left and up diagonally).

    At the moment I have a simple Input Controller with Start, Performed, Calceled events and settings a bool property of 'move button is pressed' to allow continuously sending the direction to the player controller. If I press another button while holding left for example, the controller will keep running the performed function with the left direction and will not detect the new button until I release the left button and press up again. Any ideas how to do this kind of input interaction where the script could detect the new up direction while still holding the left?

    For this I've set an Action Map : Player -> Movement -> 2D vector -> Up / Down / Left / Right keys

    Any help would be appreciated!

    Thanks
     
  2. andrew_oc

    andrew_oc

    Unity Technologies

    Joined:
    Apr 16, 2021
    Posts:
    77
    One possibility would be to make four separate actions, instead of using the 2D vector composite action. Then in your own code, using the InputAction.WasPressedThisFrame() and InputAction.WasReleasedThisFrame() methods, record the times that each of the arrow key actions is pressed and released and only call InputAction.ReadValue<float>() on the latest one and apply that value to your character movement.

    Alternatively, you could do something similar in a custom composite control. Looking at Vector2Composite as a reference, override the ReadValue method in your own implementatino, and in there, use the InputBindingCompositeContext.GetPressTime() on each of the composite parts (up, down, left, and right) and apply the same logic as above.

    Some more information on writing custom composite here controls https://docs.unity3d.com/Packages/c...ActionBindings.html#writing-custom-composites
     
    sophoni likes this.
  3. sophoni

    sophoni

    Joined:
    Jul 19, 2015
    Posts:
    5
    Fantastic, I was looking for something like this.

    Thanks!