2
\$\begingroup\$

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

enter image description here

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;
    }
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Daniel Netzer Jul 5 '16 at 11:11
  • \$\begingroup\$ I have added this code to the player.Run animation is the default animation being played.I have attached the image of it above \$\endgroup\$ – user1509674 Jul 5 '16 at 11:26
  • \$\begingroup\$ 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. \$\endgroup\$ – Daniel Netzer Jul 5 '16 at 14:36
0
\$\begingroup\$

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");
        }
    }
}
\$\endgroup\$
0
\$\begingroup\$

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;
        }
    }
\$\endgroup\$
  • \$\begingroup\$ if he need once run animation why don't you use Input.GetKeyDown!!!! \$\endgroup\$ – Seyed Morteza Kamali Jul 5 '16 at 16:11
  • \$\begingroup\$ 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" \$\endgroup\$ – user1509674 Jul 7 '16 at 4:23
  • \$\begingroup\$ @user1509674 edited the code a bit. give it a try now. \$\endgroup\$ – Daniel Netzer Jul 7 '16 at 7:20
  • \$\begingroup\$ hope its the problem with the animation file \$\endgroup\$ – user1509674 Jul 11 '16 at 6:16
0
\$\begingroup\$

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.

Instead, I would suggest this:

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).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.