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();
}
}
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");
}
}
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
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613041828im_/https://answers.unity.com/themes/thub/images/avi.jpg)