- Home /
The question is answered, right answer was accepted
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. :/ 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);
}
}
}
}
Answer by FlaSh-G · Jul 07, 2017 at 09:03 PM
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))
Ahh, thank you! :) That explanation actually helped a lot. The error is gone now, (it's not playing the animation, but I'll go and check my states before I ask again XP)
Nope :/ The error was fixed (thank you!) but the particular animation won't play. It'll play the one for 90 degrees and 0 degrees, but no 45.
Well... my answer was only about the scripting part. In terms of animating correctly, consider using a BlendTree rather than coding all the cases by hand.
I have been using the animation tree. It works when I change the states manually in the Unity editor, just not when I play in game. Thanks for your help. :)
There is no reason for it not to work ingame if you set up everything correctly, so keep trying ;)
Answer by NoseKills · Jul 08, 2017 at 09:24 AM
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
Thank you, that worked! :) For a $$anonymous$$ute... Not sure what I did. I put the code in and it worked for a while, although there was a significant delay between my pressing the buttons and the animation playing. Then $$anonymous$$onoDevelop decided to pull some formatting stunt on me, and I got it back to readability but the code didn't work after that. I've checked the states; they're the right ones.
void Update ()
{
bool isWPressed = Input.Get$$anonymous$$ey($$anonymous$$eyCode.W);
bool isDPressed = Input.Get$$anonymous$$ey($$anonymous$$eyCode.D);
wasWPressed = isWPressed;
wasDPressed = isDPressed;
//This makes the movement animation play when certain keys are pressed and released
//(works by degrees; D = 0 degrees, W+D = 45 degrees, etc.
// One of the keys changed state
if (isWPressed != wasWPressed ||
isDPressed != wasDPressed)
{
// Seems in the new state W is pressed, D is not
if (isWPressed && !isDPressed)
{
//$$anonymous$$ove at a 90 degree angle
anim.SetInteger ("State", 7);
}
// Seems in the new state D is pressed, W is not
else if (!isWPressed && isDPressed)
{
//$$anonymous$$ove at a 0 degree anle
anim.SetInteger ("State", 5);
}
// Seems in the new state both D and W are pressed
else if (isWPressed && isDPressed)
{
//$$anonymous$$ove at a 45 degree angle
anim.SetInteger ("State", 6);
}
// Neither pressed
else
{
anim.SetInteger ("State", 0);
}
}
}
Thank you for helping me ^^"