- Home /
Help playing the right animation
I am making a RPG game and my player has these animations:
STATES:
-1) walk backwards
0) idle
1) walk
2) run
3) crouch idle
4) crouch walk
5) attack
6) jump
Now for some reason i Cant make a code that has the state at the right time. The problem I am having is setting the right state with the user input. The code I am using now that DOES NOT WORK is:
void GetState ()
{
/***** STATE *****
* -1) walk backwards
* 0) idle
* 1) walk
* 2) run
* 3) crouch idle
* 4) crouch walk
* 5) attack
* 6) jump
*/
if (Input.GetAxis("Vertical") != 0)
{
if (Input.GetAxis("Vertical") > 0)
{
if (!Input.GetKey(KeyCode.LeftShift) || !Input.GetKey(KeyCode.LeftControl))
{
state = 1;
}else{
if (Input.GetKey(KeyCode.LeftShift))
{
state = 2;
}
if (Input.GetKey(KeyCode.LeftControl))
{
state = 4;
}
}
}
if (Input.GetAxis("Vertical") < 0)
{
state = -1;
}
}else{
if (Input.GetKey(KeyCode.LeftControl))
{
state = 3;
}else{
state = 0;
}
}
print (state);
anim.SetInteger ("State", state);
}
For some reason I cannot make a script that the state is correct. For example with this code the state will never become 2. Can anyone help me come up with a script that gets the state to be correct? Thank you so much
Answer by alexi123454 · Jul 20, 2015 at 08:12 PM
if (!Input.GetKey(KeyCode.LeftShift) || !Input.GetKey(KeyCode.LeftControl))
should be
if (!Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.LeftControl))
In your code, if either the left shift isn't held down OR the left control isn't held down, the state will be set to 1. You want it so that if NEITHER is held down, then the state is set to 1.
For example: I'm holding up and left shift at the same time. I'll get to that if check, and it will detect that I'm not holding down left control. Therefore, even though the result I wanted was state 2, it will give me state 1. If I was holding down up AND left shift AND left control, then I would get state 4, because it sets it to state 2, but then immediately sets it to state 4 afterwards (because there is no 'else' statement).