Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question New Input System diagonal move on single key click

Discussion in 'Input System' started by Kavado, Dec 28, 2020.

  1. Kavado

    Kavado

    Joined:
    Aug 13, 2016
    Posts:
    2
    Hello,

    I've started my journey with Unity a few days ago. For my first project, I decided to create a small 2d pixelart RPG game with grid based movement. For now, I've created a movement script using UnityEngine - StartCoroutine, Lerp etc. - its not perfect and I have to make some changes but for now I'm happy with it - works as I wanted :). My prio was to achieve diagonal movement by single key click.

    After some research I've found a lot of materials about new Input System and these are my questions:
    1. Does using it over pure script have any great advantages?
    2. It is possible to achieve somehow diagonal movement by pressing single key instead of combination of two? When I'm adding Input Action it is generating for me basic movement WSAD or ARROWS and diagonal is done by W+A, W+D and so on. Can't find a way how or if its even possible, to bind e.g
      W+A diagonal movement under just Q key.
    I will appreciate the tips!

    Regards,
    Konrad
     
  2. idbrii

    idbrii

    Joined:
    Aug 18, 2014
    Posts:
    51
    > Does using it over pure script have any great advantages?​

    You can use the new input system entirely from script! You setup your mappings in an InputAction asset and can query it from code. See SimpleDemo_UsingActionAsset.cs in the Simple Demo example from the Input System Package examples.

    If you mean better than the old input system, I'd expect the new system is more consistent across platforms? The old system doesn't support mfi controllers or Xbox dpad on macOS, anything on iOS, and has different button layouts for many gamepad types.

    If you mean using the raw Gamepad.current.gamepad.buttonSouth.wasPressedThisFrame API (seen in SimpleController_UsingState), then the benefit to InputAction is that you can support multiple input types with one configuration -- your game can support keyboard and gamepad with less work.

    > It is possible to achieve somehow diagonal movement by pressing single key instead of combination of two? When I'm adding Input Action it is generating for me basic movement WSAD or ARROWS and diagonal is done by W+A, W+D and so on. Can't find a way how or if its even possible, to bind e.g W+A diagonal movement under just Q key.​
    Use two separate bindings. Bind wasd a Vector2 like in the SimpleDemo_UsingActionAsset sample, and add qezc (or whatever your four diagonals would be) as more inputs.

    If you setup four Button inputs for each diagonal direction, you could do it like this:
    upload_2021-1-2_22-8-54.png

    And in the example SimpleDemo_UsingActionAsset.cs, modify Update to be:

    Code (CSharp):
    1. void Update()
    2. {
    3.     var look = m_Controls.gameplay.look.ReadValue<Vector2>();
    4.     var move = m_Controls.gameplay.move.ReadValue<Vector2>();
    5.     // qezc. qc are x, ze are y.
    6.     // q means -x and +y - nw
    7.     // e means +x and +y - ne
    8.     // z means -x and -y - se
    9.     // c means +x and -y - sw
    10.     move.x -= m_Controls.gameplay.diagonal_nw.ReadValue<float>();
    11.     move.y += m_Controls.gameplay.diagonal_nw.ReadValue<float>();
    12.     move.x += m_Controls.gameplay.diagonal_ne.ReadValue<float>();
    13.     move.y += m_Controls.gameplay.diagonal_ne.ReadValue<float>();
    14.     move.x += m_Controls.gameplay.diagonal_se.ReadValue<float>();
    15.     move.y -= m_Controls.gameplay.diagonal_se.ReadValue<float>();
    16.     move.x -= m_Controls.gameplay.diagonal_sw.ReadValue<float>();
    17.     move.y -= m_Controls.gameplay.diagonal_sw.ReadValue<float>();
    18.  
    19.     // Update orientation first, then move. Otherwise move orientation will lag
    20.     // behind by one frame.
    21.     Look(look);
    22.     Move(move);
    23. }
    There's probably a smarter way to do it with Composite Axis (two 1D or a 2D), but it would take some figuring out.
     
    Kavado likes this.
  3. Kavado

    Kavado

    Joined:
    Aug 13, 2016
    Posts:
    2
    Hello idbrii,

    Thank you very much for your answer!

    I have already dealt with the problem. I realized that I don't have to stick only with basic movement binding but I can also make my own, seperate bindings for diagonal movement. Now I'm trying to improve it.

    In any case, thank you very much for all the explanations, they will be helpful in further work! :)

    Greetings,
    Konrad