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: arrows with and without modifier

Discussion in 'Input System' started by nicobu, Feb 27, 2020.

  1. nicobu

    nicobu

    Joined:
    Jul 12, 2013
    Posts:
    6
    Hi all,

    I probably have a simple problem. I am trying to set up the new Input System so that i can press the arrow keys to move around and press control + arrow keys to attack in a certain direction. My Input Actions look like in the attached screenshot.

    My Controller script looks like that:
    Code (CSharp):
    1. public void OnMoveLeft(InputAction.CallbackContext context) {
    2.   if(context.performed) {
    3.     Move(Direction.West);
    4.   }
    5. }
    6. public void OnForceAttackWest(InputAction.CallbackContext context) {
    7.   if(context.performed) {
    8.     Debug.Log(string.Format("OnForceAttack{0}", "West"));
    9.   }
    10. }
    Pretty straight-forward. However, when I press leftCtrl+arrow key, the movement AND the force attack execute. How can I prevent that?

    Thanks
    Nico
     

    Attached Files:

  2. nickleplated

    nickleplated

    Joined:
    Nov 2, 2018
    Posts:
    26
    nicobu likes this.
  3. nicobu

    nicobu

    Joined:
    Jul 12, 2013
    Posts:
    6
    Thanks!
    I did actually end up with a much easier solution. I added a ToggleAttackMode Action and changed my code like this:
    Code (CSharp):
    1. public void OnToggleAttackMode(InputAction.CallbackContext context) {
    2.   if(context.started) {
    3.     // button pressed
    4.     Mode = PlayerMode.Attack;
    5.   }
    6.   if(context.canceled) {
    7.     // button released
    8.     Mode = PlayerMode.Default;
    9.   }
    10. }
    11.  
    12. public void OnActionLeft(InputAction.CallbackContext context) {
    13.   if(context.performed) {
    14.     if(Mode == PlayerMode.Default) Move(Direction.West);
    15.     else if(Mode == PlayerMode.Attack) {
    16.       if(EquipmentSlots.AttackType == AttackType.Melee) MeleeAttack(Direction.West);
    17.       else if(EquipmentSlots.AttackType == AttackType.Ranged) RangedAttack(Direction.West);
    18.     }
    19.   }
    20. }