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.
; expected after (“ctrl”))
Invalid expression term &&
ArgumentException: Input Key named:down is unknown
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.
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.
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.