- Home /
Sprint animation play ONLY when speed = 13
So i want my sprint animation to play when holding up AND shift , and once the maxwalk speed reaches 13 , the sprint animation plays ... I've been stuck on this for nearly 4 months now ....
function Update ()
{
if(Input.GetButtonDown("sprint"))
{
isSprinting = true;
ump.animation.Play("sprintAnimation");
movement.maxForwardSpeed = 13;
}
if(Input.GetButtonUp("sprint"))
{
isSprinting = false;
movement.maxForwardSpeed = 6;
ump.animation.Stop("sprintAnimation");
}
}
I know this is wrong but i've tried my best , Any help will be appreciated :)
thanks for reviewing my post... here's my advice
the two statements appear repetitive... rather than checking if the button is released, why not make the else statement ALWAYS set sprinting to false?
don't forget to crossfade your animations if your using 3d.
and if you want sprint active ONLY if your speed is at 13 (meaning 13 and above) you should include that in your if statement.
if(Input.GetButtonDown("sprint") && movement.maxForwardSpeed>=13) { perform sprint animation } else { sprint=false; }
in this case the bool sprint isn't nessicary
Thank you alot :) , i will let you know if this works
the two statements appear repetitive... rather than checking if the button is released, why not make the else statement ALWAYS set sprinting to false?
That is not true. GetButtonDown() and GetButtonUp() are true in the frame when the user presses/releases the button, not when he just holds the button down. Do not confuse them with "GetButton()"
In his code it appeared he wanted to sprint only if he had been moving in a forward velocity.once move speed reached 13 it would auto show the sprint animation. Ivy seen this in sonic unleashed when daytime sonic ran an uninterrupted course for x amount of time.
I never used the phrase "getbutton()". Thanks for revising my advice. As you noticed I didn't answer his question, but only commented to attempt helping him think another structure for his problem.
Answer by Immanuel-Scholz · Nov 08, 2013 at 04:16 PM
From your words, its not entirely clear to me what you want and what you currently have. Maybe you can rephrase your question, if the following is not correct:
So you want to start playing the animation "sprintAnimation" if both of the following is true:
The user presses the upward key
The user holds down shift
Also, together with starting "sprintAnimation", you want to set the maximum speed of the avatar to "13". (The speed is then probably used from somewhere else to actually move the character.)
Is this correct?
If so, first make sure that the following things are configured correctly:
You have a virtual button mapping for your key "sprint" in Edit/Project Settings/Input, which maps the main key to "right shift" and the alternative key to "left shift"
You have another axis mapping for your movement. Or you use the default setting of "Vertical" for that (should map to up/down arrow keys)
If that's taken care of, change your code to not only test for the keydown/up event on "sprint" but also on the axis key.
if(Input.GetButton("sprint") && Input.GetAxis("Vertical") > 0.5f)
...
if(!Input.GetButton("sprint") || Input.GetAxis("Vertical") <= 0.5f)
Please note that I do not use GetButtonDown/Up
but instead GetButton
. The *Down/Up version only returns true in the one single frame when the user presses/release the corresponding key. That is undesireable for you, since your user doesn't need to release/press the up-key and the shift-key in the same frame.
JESUS YOU LEGEND!!!!!!!!!!!!!!!! , I am VERY happy with what you said , you got everything correct and i am very thankful for that! Thumbs up
Your answer
![](https://koobas.hobune.stream/wayback/20220613121435im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Why Isn't This Sprint Working? 1 Answer
Increase/reset speed on tilt 0 Answers
Scroll to change walk speed... 1 Answer