# Animation clip not working

I am working on playing different animation of key press.Below are the animation element attached to my player

Initially run animation is played.I have written code to play "Jump" animation on keypress.

The problem is that when I press the key "Jump" animation will be played,But this animation is played again and again.

I need to play the "Jump" animation only once.Then I need to switch to run animation

Here is the code written

public bool run=false;

void Update () {
if(Input.GetKey(KeyCode.R))
{
run = true;

}
if (run == true) {

GetComponent<Animation>().Play("Jump");
GetComponent<Animation>().wrapMode = WrapMode.Loop;
}
}

• its an infinite loop, think about it. if(run == true) is always true, you never change run to false so it will jump none stop the second you press R first time. where do you activate the run Animation? if you can add some more of the code it shouldent be a problem to solve this. – Daniel Netzer Jul 5 '16 at 11:11
• I have added this code to the player.Run animation is the default animation being played.I have attached the image of it above – user1509674 Jul 5 '16 at 11:26
• and you want your character to jump when it starts running? when the player clicks R once, you jump and then start running? anyway ill add a solution to this but i'm not sure the way you implemented this is idle. – Daniel Netzer Jul 5 '16 at 14:36

if you want play once Jump animation you can use Input.GetKeyUp for falsing condition.your way isn't correct because you Check condition in Update function.In update function while condition is true do your work. using UnityEngine; using System.Collections;

public class AnimationComponent : MonoBehaviour {

void Start () {
GetComponent<Animation>().Play ("Walk");
}

void Update () {
if (Input.GetKeyDown (KeyCode.R)) {
GetComponent<Animation> ().Play ("Run");
GetComponent<Animation> ().wrapMode = WrapMode.Loop;
}
if (Input.GetKeyUp (KeyCode.R)) {
GetComponent<Animation> ().Play ("Walk");
}
}
}


at the moment the loop that run the Jump animation is infinite because

if (run == true) { // this condition is always TRUE once the player press R.


we can avoid it by changing that into a diffrent boolean that doesnt effect the running animation itself as I can only guess that when RUN equals TRUE the player is running so the following code should solve this problem.

public bool run = false;

void Start () {

}

void Update () {
if(Input.GetKey(KeyCode.R))
{
run = true;
GetComponent<Animation>().Play("Jump");
GetComponent<Animation>().wrapMode = WrapMode.Loop;
}
}

• if he need once run animation why don't you use Input.GetKeyDown!!!! – Seyed Morteza Kamali Jul 5 '16 at 16:11
• I tried the above code.But when I press "R" on my keyboard it plays the animation Jump but the jump animation is played continuously.Its not going back to its default stae "Run" – user1509674 Jul 7 '16 at 4:23
• @user1509674 edited the code a bit. give it a try now. – Daniel Netzer Jul 7 '16 at 7:20
• hope its the problem with the animation file – user1509674 Jul 11 '16 at 6:16

There are parts of both Seyed's answer and Daniel's answer that are correct.

However, I would advocate for different code.

For example, Daniel does this:

public bool run = false;

void Update () {
if(Input.GetKey(KeyCode.R)) {
run = true;
GetComponent<Animation>().Play("Jump");
GetComponent<Animation>().wrapMode = WrapMode.Loop;
}
}


Which doesn't fix the issue that run will be true forever (the identified problem). All it does is make the effects of that no longer dependent on it.

void Update () {
bool run = false;
if(Input.GetKey(KeyCode.R)) {
run = true;
}
if(run) {
GetComponent<Animation>().Play("Jump");
GetComponent<Animation>().wrapMode = WrapMode.Loop;
}
}


Which makes the scope of run function scope rather than class scope (which lets it reset) or use an else statement that resets it, or remove it entirely (as doing it purely based on the input is fine, just that the field is no longer relevant). What you do depends on whether or not you're accessing that field from outside the class or in another method.

Then, both Daniel and Seyed are still using Input.GetKey(KeyCode.R), which means that the key is not rebindable and if you ever decide that "You know what, I don't want Run to be R, I want it to be Left Shift" you have to change it inside your code anywhere and everywhere you're using KeyCode.R.

Instead use Input.GetButton("Run") and use the input settings (Edit -> Project Settings -> Input) menu to configure your keyboard input. Alternatively you can get an asset like Rewired that allows you to do key rebinding in game rather than Unity's setup which prompts the user before the game and looks wholly unprofessional (and it only works for Windows and Mac builds, webgl and console builds won't show it and your keybinds are again permanent).