Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by KristoDaHeisko · Nov 30, 2016 at 02:57 AM · c#animationtriggernoobgetkeydown

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 palt textress 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. alt text 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);
     }
 }

}

sanic.png (405.6 kB)
sanic20.png (420.3 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThePersister · Nov 30, 2016 at 10:06 AM 0
Share

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!

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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: Idle to Running, shows Bool Parameter and usage

Running To Idle (Note how the condition is false): Running to Idle, shows false as condition

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


idletorunning.jpg (160.1 kB)
runningtoidle.jpg (156.2 kB)
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThePersister · Nov 30, 2016 at 09:08 PM 0
Share

Hey @$$anonymous$$ristoDaHeisko, Thanks for accepting my answer and keeping it that way haha!

If you need anything else, let me know! :)

avatar image KristoDaHeisko · Nov 30, 2016 at 09:39 PM 1
Share

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!

avatar image KristoDaHeisko · Nov 30, 2016 at 11:00 PM 1
Share

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!!

avatar image ThePersister KristoDaHeisko · Dec 01, 2016 at 09:13 AM 0
Share

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

avatar image tcz8 ThePersister · Dec 01, 2016 at 03:36 PM 1
Share

Ill return the favor, thanks.

avatar image
1

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
     }    


Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tcz8 · Nov 30, 2016 at 09:03 PM 0
Share

Come to think of it, to be optimal, you may want to move movement = (Input.GetAxis ("Vertical")); inside if (inputAllowed) {, right at the begining.

avatar image KristoDaHeisko · Nov 30, 2016 at 09:46 PM 1
Share

thank you so much man! ill be sure to try out the pause menu and i am thankful for your response!

avatar image KristoDaHeisko · Nov 30, 2016 at 11:05 PM 1
Share

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!

avatar image tcz8 KristoDaHeisko · Dec 01, 2016 at 05:45 AM 0
Share

I guess I'll just have to wake up earlier if I want to catch replies to my own questions.

avatar image
0

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 alt text

alt text


1.png (64.3 kB)
2.png (65.0 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

308 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ANIMATION STRANGELY DOES NOT PLAY 0 Answers

Animation triggered by other animation, script, c# 1 Answer

How do I not trigger animation during play state of the triggered animation. 1 Answer

ANIMATION TRIGGER DOESN'T PLAY 1 Answer

AnimationComplete() Error 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges