C# GetKeyDown not working
sorry im new with animation and coding. i'm trying to program sanic to go from the idle animation to the running animation when the w key is being held down. I've managed to make the running animation work but, it continues to loop on the running animation even after i let my finger up off the w key.
if i press w again when it is looping it will stop playing the running animation and sanic will slide around with the idle animation until i press w again and it will go back to running loop. i don't understand this because isn't GetKeyDown only suppose to be true if the key is being pressed? it is acting like just the normal GetKey and just toggling like a on/off switch.
this is not exactly 100% my original code. i watched a video on how to make it but i edited parts to suit my fancy.. sorry if it is a dumb question. i'm reeeealy new to coding. v code v
using UnityEngine; using System.Collections;
public class animations : MonoBehaviour {
static Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent <Animator>();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("w")) {
anim.SetBool ("isRunning", true);
}
if (!Input.GetKeyDown ("w"))
{
anim.SetBool ("isRunning", false);
}
}
}
Try using Get$$anonymous$$eyUp ins$$anonymous$$d for starters. Never seen or tried anything like an unnecessarily inverted get$$anonymous$$eyDown statement, haha.
static Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent <Animator>();
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$eyDown ("w")) {
anim.SetBool ("isRunning", true);
}
else if (Input.Get$$anonymous$$eyUp ("w"))
{
anim.SetBool ("isRunning", false);
}
}
See if that works, since you do run when pressing W, supposedly it should. Also check if you're not influencing the isRunning variable of that animator in any other place.
You might also want to check how you're using the Animator. Show a screenshot of the animator window, whilst having the return arrow selected. That way we can see if the Exit state of going back to walking is the following:
"isRunning" => "False"
That's what it should be. On the arrow from Running back to Walking "Exit Conditions" => "isRunning" => "False".
Let me know how that goes!
Answer by ThePersister · Nov 30, 2016 at 02:34 PM
Hi @KristoDaHeisko,
Looks like it should be a simple fix!
What you currently have in your animator is probably a Trigger Parameter. What you want is a Bool Parameter.
As shown below, you can see the difference (radio button (Trigger) vs checkbox (Bool)):
Idle To Running:
Running To Idle (Note how the condition is false):
So change your parameter to a Bool parameter and it should all work out! I hope it does, best of luck!
If this helped, please accept this answer, it'd be much appreciated! :)
If you need any more details, let me know!
Cheers,
ThePersister
Hey @$$anonymous$$ristoDaHeisko, Thanks for accepting my answer and keeping it that way haha!
If you need anything else, let me know! :)
thank you very much. works like a charm now that i changed the trigger to a bool and the inverted GetButtonDown into a if else get button up. thank you very much mate!
i'm sorry lol i felt bad for not giving any thanks to tcz8 then felt bad again for not giving thanks to you and did that a couple times. but my decision is final and you have been extremely helpful. thank you!!
Haha, no worries, I appreciate it a lot. You can always upvote answers to give @tcz8 some credit, I'll give him some too for good manners.
I'm glad I could help out! Best of luck with future issues! :)
Cheers,
ThePersister
Answer by tcz8 · Nov 30, 2016 at 04:11 AM
Have you triple checked all the conditions to enter and leave states in the animator controller?
EDIT: ThePersister is right, you should use Input.GetKeyUp and there is the issue with using SetBool in your code while your Animation controller is setup with triggers. Fix that and it should work.
Additionally, I would suggest changing your code to use unity's virtual input instead, it will make your life easier and allow you to remap your inputs easily.
BTW, how do you move your character? are you using root motion or modifying its transform?
Either way you can modify your code to use the virtual inputs now if you want. The code bellow is derived from what I use. You get a pause function as a bonus. I suggest going into EDIT > Project Settings > Input and adding an entry for a Pause button. This way you can assign whatever you want instead of Fire3's default which you may require in game for something else. FYI the name field is what you must match in your code to detect the input.
See this link for the proper key names to use in the input manager
public GameObject pauseMenu; // Gameobject that contains the pause menu Canvas and UI
bool inputAllowed = true; // Use to disable player input for pause menu or cinematics
bool gamePaused = false; // Track the game paused state
float currentTimeScale; // Use to save the timescale to restore when comming out of pause
float movement; // Used to track the movement input
static Animator anim;
void Awake () {
// Make sure pause menu is hidden on start (same as disabling an obj in the inspector)
pauseMenu.SetActive(false);
}
void Start () {
anim = GetComponent <Animator>();
}
void Update () {
// By default Vertical = W & S Horizontal=A & D, should work with gamepad right away
movement = (Input.GetAxis ("Vertical"));
if (inputAllowed) {
// If movement is positive (-1=negative) and not 0 (0=positive for Mathf.Sign)
if (Mathf.Sign(movement) == 1 && movement != 0)
anim.SetBool ("isRunning", true); // Start the anim
else // If movement is negative or 0
anim.SetBool ("isRunning", false); // Stop the anim
}
// By default Fire3 = left shift on keyboard or button 2 on gamepad
if (Input.GetButtonDown ("Fire3"))
PauseGame();
}
void PauseGame () {
if (!gamePaused) { // If game is not paused
currentTimeScale = Time.timeScale; // Save the timescale
Time.timeScale = 0; // Pause time flow, who's the Doctor now!
pauseMenu.SetActive(true); // Enable the pause menu obj
} else { // If game is paused
Time.timeScale = currentTimeScale; // Restore the saved timescale
pauseMenu.SetActive(false); // Disable the pause menu obj
}
gamePaused = !gamePaused; // Toggle between true and false
}
Come to think of it, to be optimal, you may want to move movement = (Input.GetAxis ("Vertical")); inside if (inputAllowed) {, right at the begining.
thank you so much man! ill be sure to try out the pause menu and i am thankful for your response!
i did what you guys said and it worked very well! you've been very helpful. sorry i had to flip a coin to decide who i would accept. ill be sure to ask you another dumb question some time lol have a good night m8!
I guess I'll just have to wake up earlier if I want to catch replies to my own questions.
Answer by KristoDaHeisko · Nov 30, 2016 at 01:05 PM
i checked again everything seems to be in order.. at least to me. i could be missing something simple though. the idle is transitioned to the running and the running is connected back to the idle... @tcz8
here are some screenshots
Your answer
![](https://koobas.hobune.stream/wayback/20220612101714im_/https://answers.unity.com/themes/thub/images/avi.jpg)