Multiple key inputs

I am trying to get some attacking animations only work in the corresponding direction.
Player can attack, up, right, down and left, so I only want the attackUp to be used when the player is looking up and then attack - and so on and so fourth.

A way i thought i could do this is by using multiple key commands but so far I’ve have no luck.

if (Input.GetKeyDown("down"))
        {
            objAnimator.SetTrigger("WalkDownTrigger");
        }


        if (Input.GetKeyDown("down ")) && (Input.GetKeyDown("ctrl"))
        {
          
            objAnimator.SetTrigger("AttackDown");
        }

I thought this would work because the down key for walking followed by attacking would make sure the player couldn’t attack in a direction the player ins’t facing.

3 errors are being reported.

  1. ; expected after (“ctrl”))
  2. Invalid expression term &&
  3. ArgumentException: Input Key named:down is unknown

Any advice is greatly appreciated.

You have one too many ) on line 7.
You should also probably replace GetKeyDown with GetKey on the control key, as you are unlikely to start pressing both keys down in the same frame.
I prefer using the KeyCode version of GetKey*, as that avoids string comparisons and throws a compile error if you input a key that does not exist.

1 Like

I have attached the errors, as I dont understand why its asking for more brackets, and how does GetKey work?
Is it the same but it doesnt matter when key is pressed?

The whole script.

using UnityEngine;
using System.Collections;
public class PlayAnimation : MonoBehaviour
{
    private Animator objAnimator;
   
    void Start()
    {
        objAnimator = GetComponent<Animator>();
    }
   
    void Update()
    {
        if (Input.GetKeyDown("right"))
        {
            objAnimator.SetTrigger("WalkRightTrigger");
        }

        if (Input.GetKeyDown("left"))
        {
            objAnimator.SetTrigger("WalkLeftTrigger");
        }

        if (Input.GetKeyDown("up"))
        {
            objAnimator.SetTrigger("WalkUpTrigger");
        }

        if (Input.GetKeyDown("down"))
        {
            objAnimator.SetTrigger("WalkDownTrigger");
        }


        if (Input.GetKeyDown("down") Input.GetKey("ctrl"));
        {
           
            objAnimator.SetTrigger("AttackDown");
        }

    }
}

GetKey returns true for each frame the key is pressed, whereas GetKeyDown only returns true for the first frame. So, for example, while you’re pressing the down key, if you tap ctrl you’ll attack.
On line 35 you are missing && which would be causing the errors. Also, I’m with ThermalFusion on preferring to use KeyCode rather than the key string. Helps prevent typo’s and ambiguity.

Looking at this, I think there might be a better way to handle attacking in certain directions, rather than taking over movement keys for directional attacks. Since you only want the character to attack in the direction they are facing, why not have the attack match the object orientation? That way, you only need to use the ctrl key to attack and don’t need to worry about using a direction key.

That does sound better but how do I define the orientation of the player position?
and for keycode would that be ‘LeftControl’ instead of leftctrl ?

Yes, LeftControl is the keycode.
As for how you’d define player position, it kind of depends on your game, and how your character is being moved. If you’re rotating your character, then you can tie the attack to the characters forward axis (I think it’s usually X)
Or you could have a string variable and so each time the player presses one of the directions, the variable gets set to the corresponding direction. When ctrl is pressed, you already know which direction the character is facing.

It is a top down 2D game so attacking directions are Up, right, down and left.

i spend aaaages trying to fix mine, thank you did not realise having both set to down i would have to be pressing them at the exact same time