Delay when Animation used

So I’ve been working on character movement for an isometric game, where I want the character to have an animation for moving 0 degrees, 45 degrees and 90 degrees. 0 and 90 have been working just fine, but I’ve have trouble with 45 as it requires two buttons to be pressed simultaneously, and I don’t know the code for adding button input together, nor can I find it. :confused: Help would be appreciated!

{
//I've tried using +
		if (Input.GetKeyDown (KeyCode.W) + (KeyCode.D))
		{
			anim.SetInteger("State", 6);
		}
//And I found && online,
			if (Input.GetKeyUp (KeyCode.W) && (KeyCode.D))
		{
			anim.SetInteger("State", 0);
		}
}

but both give me errors such as "Operator ‘+’ cannot be applied to operands ‘bool’ and ‘UniyEngine.KeyCode’

Again, help would be great. Thanks!

EDIT: Ok, the error was fixed but the particular animation won’t play. It’ll play the one for 90 degrees and 0 degrees, but no 45.

EDIT AGAIN: With help (thank you) I managed to get the animation to play when both buttons are pressed down, unfortunately there was a significant time delay between pressing the buttons and the animation playing, except for the “D” key. Any solutions?

using UnityEngine;
using System.Collections;

public class Player_Animator_Control : MonoBehaviour
{

	Animator anim;

	// Use this for initialization
	void Start ()
	{
		anim = GetComponent<Animator> ();
	}

	// Update is called once per frame
	void Update ()
	{
 		{		
			// Seems in the new state both D and W are pressed
			if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
			{		
				//Move at a 45 degree angle
				anim.SetInteger ("State", 6);
			}
			// Seems in the new state W is pressed, D is not
			else if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.D))
			{		
				//Move at a 90 degree angle
				anim.SetInteger ("State", 7);
			}
			// Seems in the new state D is pressed, W is not
			else if (!Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
			{		
				//Move at a 0 degree anle
				anim.SetInteger ("State", 5);
			}
			// Neither pressed
			 else
 			{		
				anim.SetInteger ("State", 0);
			}
		}
	}
}

A method call looks like this:

NameOfTheMethod(parameters)

And your code has that:

Input.GetKeyDown(KeyCode.W)

which is fine. This method call returns a bool, true or false, depending on whether or not the given key is pressed at the moment. It’s kinda like

IsReady(steak)

Then, you add this behind your method call:

+ (KeyCode.D)

so you basically have

IsReady(steak) + potato

If the steak is ready, you get

true + potato

and oh boy, is that a complicated question to answer, right? Same with &&, which is, by the way, the correct operator to use here, being “AND”. But still:

is true and potato?

Kinda hard. So what you want to do is to ask if the steak is ready and then you ask if the potato is ready, too:

IsReady(steak) && IsReady(potato)

which evaluates to

true && true

and this works. True is kinda like “yes”, so we have

if yes and yes

which seems more easily to analyse. And, in fact, would be correct code. Well… semantically.

Long story short:

if(Input.GetKeyDown(KeyCode.W) && Input.GetKeyDown(KeyCode.D))

On top of what @FlaSh-G said, there’s the problem that GetKeyDown and GetKeyUp return true during that 1 frame during which the key becomes pressed or released. So…

if(Input.GetKeyDown(KeyCode.W) && Input.GetKeyDown(KeyCode.D))

means that those keys need to be pressed down (or released) during the same frame. If you press them at even slightly different times, the condition won’t be true.

Since the animation you want to play depends on the state of the 2 buttons, you should monitor them and act if they change.

bool wasWPressed;
bool wasDPressed;

void Update() {
    bool isWPressed = Input.GetKey(KeyCode.W);
    bool isDPressed = Input.GetKey(KeyCode.D);

     if (isWPressed != wasWPressed ||
          isDPressed != wasDPressed) {
        // One of the keys changed state

        if (isWPressed && !isDPressed) {
             // Seems in the new state W is pressed, D is not
        } else if (!isWPressed && isDPressed) {
             // Seems in the new state D is pressed, W is not
        } else if (isWPressed && isDPressed) {
             // Seems in the new state both D and W are pressed
        } else {
             // Neither pressed
        }
     }

    wasWPressed = isWPressed;
    wasDPressed = isDPressed;
}

Then just trigger the right anim in the right if/else