Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by nictm · Aug 18, 2021 at 03:35 PM · keycode

Disable a KeyCode

Hello,

I'm looking for a way to disable a keycode, specifically "Q", while I have an amination running. This is what I found online, but it is an old code and I can't get it to work. Any help would be greatly appreciated!!

Thanks, Nic

      void Update()
             {            
                 if (this.myAnimator.GetCurrentAnimatorStateInfo(1).IsTag("StandingSwordAttack")
                 {
                     DisableKey(KeyCode.Q);
                 }
             } 
 
            void DisableKey(KeyCode key)
             {
                   if(Event.current.keyCode == key  (Event.current.type == EventType.KeyUp || 
                   Event.current.type == EventType.KeyDown)) 
                   {
                        Event.current.Use();
                   }
              }

Comment
Add comment · Show 3
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 Hellium · Aug 18, 2021 at 03:46 PM 0
Share

You need to explain what do you mean by "disabling a keycode".

If you have somewhere else the Q key used to trigger something, you can simply use a boolean.

          bool canPerformActionWithQ = true;

          void Update()
          {            
              canPerformActionWithQ = myAnimator.GetCurrentAnimatorStateInfo(1).IsTag("StandingSwordAttack") == false;

              if(Input.GetKeyDown(KeyCode.Q) && canPerformActionWithQ)
              {
                  Debug.Log("Q pressed, perfor$$anonymous$$g action");
              }
          } 
avatar image nictm · Aug 18, 2021 at 05:29 PM 0
Share

Thanks for responding. Q is my action key. It used many places in my game. What I'm trying to do is while I'm running, not to be able to input the Q button, otherwise when I push Q while running to interact with a chest for example, I continue to run and the game messes up. I have many different runs/jumps/interactions, so instead of putting the code on each individual object or line of code for different actions, I figured if I was unable to input Q while running, jumping or whatever that would fix the issue. I just have no idea how to have the Q key do nothing when I push it. Thanks! @Hellium

avatar image rage_co nictm · Aug 19, 2021 at 04:20 AM 0
Share

there can be two other options if you want to make a gamewide setup for this, one of them is creating your own input manager which interprets the input in the way you want before giving the output-input, but this is a bit unnecessary in more cases...you can just create a singleton game manager and create two functions....SetStatus(KeyCode key, bool active) and GetEnabled(KeyCode key)...and then create a array of a serialized class which holds a key and whether it's enabled or disabled...and setup the functions to set their respective classes booleans true or false....and then simply use && GetEnabled(KeyCode.Q) with Input.GetKeyDown............this too might be a bit overkill for smaller games, but if you think this might help you...ill help you set this up

0 Replies

· Add your reply
  • Sort: 

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

166 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

Related Questions

Unity does not register first key press 1 Answer

how to activate a power up by pressing down a key? 0 Answers

Is there anyway to force Keycode.w through UI button? 0 Answers

Handle keyboard keys beyond KeyCode 0 Answers

Question about Input.GetKey 1 Answer


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