- Home /
 
Deactivate Key.
So basically I made a script where when the "G" key is Held it plays an animation and disables the Polygon Collider 2d, and I made another script that basically allows you to run or walk. How do I make it when other Keys are being pressed or held then if the G key is held it does nothing.
-So basically how can I make so if player is pressing/holding other Keys then the "G" key won't activate'
Answer by $$anonymous$$ · Aug 30, 2017 at 07:14 AM
 private bool disableG; // this will be set to true or false
 private void Start()
 {
     disableG = false;// we start it at false
 }
 private void Update () {
     if (Input.anyKeyDown) // if you press down any key
     {
         if (!Input.GetKeyDown(KeyCode.G)) //and that key isn't g
         {
             disableG = true; //disable g is set to true
         }
     }
     if (!Input.anyKey) //if you're not pressing anything
     {
         disableG = false; //disable g is set to true, this will ensure if you press and release a button you'll be able to press g again even if it's in a single fram
     }
     if (Input.GetKeyDown(KeyCode.G) && disableG == false) //now we see if you're pressing g and it hasn't been disabled by pressing another button
     {
         //do stuff
     }
 }
 
              It says it right there after // whats going on
Answer by alextejada · Jun 13, 2021 at 08:09 AM
As simple as:
 if (!Input.GetKey(KeyCode.G))
  {
          //do nothing or return
 }
 
               ,As simple as: if (!Input.GetKey(KeyCode.G)) { //do nothing or return }
Your answer
 
             Follow this Question
Related Questions
Input Manager (From string to keycode) 3 Answers
Unity will not run when I assign an animation to a keystroke 0 Answers
wait seconds before it's possible to press a key 1 Answer
Simple Character Animations 2 Answers
Moving Animations 3 Answers