- Home /
The question is answered, right answer was accepted
Double key combination problem
So here is my problem. I'm writing an animator controller and I'm trying to use a combination of two keys for sprinting animation. Thing is they work only one way, for example if I press W and then LeftShift, everything is just peachy, but when I do the presssing in a reversed order, it just doesn't work. Here's the code:
if (Input.GetKey (KeyCode.LeftShift)) {
if (Input.GetKey (KeyCode.W)) {
anim.SetBool ("isWalking", true);
anim.SetBool ("IsRunning", true);
Debug.Log ("isWalking = true");
Debug.Log ("IsRunning = true");
} else {
anim.SetBool ("isWalking", false);
anim.SetBool ("IsRunning", false);
Debug.Log ("isWalking = false");
Debug.Log ("IsRunning = false");
}
} else {
if (Input.GetKey (KeyCode.W)) {
anim.SetBool ("isWalking", true);
anim.SetBool ("IsRunning", false);
Debug.Log ("isWalking = true");
Debug.Log ("IsRunning = false");
} else {
anim.SetBool ("isWalking", false);
anim.SetBool ("IsRunning", false);
Debug.Log ("isWalking = false");
Debug.Log ("IsRunning = false");
}
}
I know that the code is rather long and one if clause would do the trick because I already tried that. But I had the same problem back then and I thought that maybe it's a compilator issue with the order of conditions inside the if clause. That's why I arrived with this solution that doesn't help either. On a side note, I have the animator set up the way that you need both isWalking and isRunning conditions to be true to get the running animation.
So an example of how this works upon testing: I press down W, the character walks, while still pressing W i press down the LEFT SHIFT key, the character runs. I let go of the LEFT SHIFT key, character walks again, I press LEFT SHIFT again, the character runs again. But when I either start with LEFT SHIFT key or let go of W while running and then press W again the animations do not play. It's not a key issue, because I tried using different keys instead of LEFT SHIFT. Funny thing is, I get the Debug.Log ("isWalking = true") and Debug.Log ("IsRunning = true") inside the console. Any suggestions?
The code seems to be correct, I have a feeling it is an issue with how the animation controller is set up. Does the running state depend on the walking state or reverse? In that case it is possible that the animation controller does not detect the change of both variables at the same time. You can look at the animation controller at run-time and see into what state it wants to go.
(I'm pretty sure it is an animation controller logic issue, because the only difference between pressing W first or L-shift is that in the second case both variables change at the same time)
Observing your state machine whilst it is running should point to the issue. I think it is probably something to do with how the transition conditions are configured and the flow of the States.
Answer by dee2061 · Jan 18, 2018 at 12:38 PM
Yeah, it turned out that I messed up transitions from idle to running, that's why idle to walking was working, and walking to running was ok too. THX for your help.
Answer by Diukrone · Jan 17, 2018 at 06:58 PM
//Pseudo code, maybe it can help!
if (Input.GetKey (KeyCode.LeftShift) && Input.GetKey (KeyCode.W))
{
if(anim.SetBool ("isWalking") == false && anim.SetBool ("isRunning") == false)
{
anim.SetBool ("isWalking", true);
anim.SetBool ("isRunning", true);
}
else
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", false);
}
}
else
{
if (Input.GetKey (KeyCode.W))
{
anim.SetBool ("isWalking", true);
anim.SetBool ("IsRunning", false);
}
else
{
anim.SetBool ("isWalking", false);
anim.SetBool ("IsRunning", false);
}
}
}