- Home /
Cannot get sprint animation to play when crouch works fine?
I have a script I wrote to play the walk animation when walking, sprint when sprinting, etc. I have it set up to where there are walk, crouch, and sprint variables all set to 0 and when you press the w, c, or left shift buttons the variables are changed to 1. The game then looks at the variables and depending on which ones are 1 and which are 0 plays the correct animation. For example, if I wanted to play the crouch animation it looks for the walk variable being equal to the couch variable, and it also looks to see if the crouch variable is 1. This means you have to be pressing BOTH the w and c keys to play the animation (the animation only plays when the character is crouched and is moving forward). I did the EXACT same code for the sprint animation as well and only changed crouch to sprint and the crouch animation to the sprint animation so it does the exact same thing but with sprinting vs crouching and IT DOES NOT WORK. The animation will not play, even though the code is exactly the same save for sprint substituted in for crouch where needed. In unity, the variables for walking and sprinting both change to 1 like they should and revert back to 0 when the keys are released (which is EXACTLY the same as when crouching), but only the crouch animation works, not the sprint. Can anyone find where I went wrong in the code and how to fix it so I can sprint properly???
function OnEnable()
{
animation.Stop("sprintAnimation");
animation.Stop("walkAnimation");
animation.Stop("crouchAnimation");
}
var crouch : float;
var walk : float;
var sprint : float;
function Start ()
{
crouch = 0;
walk = 0;
sprint = 0;
}
function Update ()
{
if(Input.GetKeyDown("space"))
{
animation.Play("jumpAnimation", PlayMode.StopAll);
}
if(Input.GetKeyDown("w"))
{
walk = 1;
}
if(Input.GetKeyDown("c"))
{
crouch = 1;
}
if(Input.GetKeyDown("left shift") || Input.GetKeyDown("right shift"))
{
sprint = 1;
}
if(walk == crouch && crouch == 1)
{
animation.Play("crouchAnimation", PlayMode.StopAll);
} if(walk == sprint && sprint == 1)
{
animation.Play("sprintAnimation", PlayMode.StopAll);
} if(walk == 1 && crouch == 0)
{
animation.Play("walkAnimation", PlayMode.StopAll);
}
if(Input.GetKeyUp("w"))
{
walk = 0;
animation.Stop("walkAnimation");
}
if(Input.GetKeyUp("c"))
{
crouch = 0;
animation.Stop("crouchAnimation");
}
if(Input.GetKeyUp("left shift"))
{
sprint = 0;
animation.Stop("sprintAnimation");
}
}
Note: I changed the left shift keybind to f incase the left shift was messing something up and nothing. I also replaced the working crouch animation with the sprint animation and it worked perfectly fine, so it isn't the sprinting animation