- Home /
Disable a Key after shooting
I got the code to disable a key when the event is happening from this link, http://forum.unity3d.com/threads/25033-how-to-disable-a-keyboard-key
What I want is, disable the up arrow key after using it once, and then when the object instantiated comes back to the spawnlocation, to activate it again to throw the object.. how do i do this??
Please Helppp!! I have tried triggers to check trigger enter and trigger exit doesnt help deactivating.. the object still spawns.. I Need Helppppp!!!
Thanks in advance..
Answer by Bovine · Jul 24, 2012 at 08:55 PM
Something like:
public bool IsKeyEnabled_W { get; set; }
void Awake()
{
IsKeyEnabled_W = true;
}
void Update()
{
if(IsKeyEnabled_W)
{
if(Input.GetKeyUp(KeyCode.W))
{
// *** DO SOMETHING HERE!!!! ***
}
}
}
And then when you wish to disable the key do:
IsKeyEnabled_W = false;
And to enable it again:
IsKeyEnabled_W = true;
Does that make sense?
no worries. You could get a bit cleverer with this if you wanted so that you didn't have to have a property per key - have a dictionary of key codes to booleans and you could use one pair of methods to enable and disable keys. I don't know how many key codes there are, but you could also used a fixed size array to do the same - a little more memory hungry but a bit more efficient.
True.. but currently I only have 4 keys in the game.. but will keep that in $$anonymous$$d for future purposes.
Answer by DaveA · Jul 24, 2012 at 08:47 PM
I wouldn't use that script, but the other suggestion there, which is, just don't respond to the key. So you have, in Update or OnGUI, some code you want to do something when UpArrow is hit. And you know some condition under which you don't want to respond. So don't. Eg
if (itsOkToRespondToUpArrow && Input.GetKeyDown(KeyCode.UpArrow))
{
... do the uparrow thing...
} // else not
Either set such a boolean variable, or make whatever function calls or check whatever variables are involved like that.
I agree, that looks clever but maybe a little too clever for my liking!
Answer by Sudhi · Jul 24, 2012 at 09:07 PM
yeyeeee... as soon as u said boolean, I got the idea.. I add a new variable
var useKeyBoard = true;
if(Input.GetKeyUp(KeyCode.UpArrow)) {
if(useKeyBoard == true) {
//code logic
useKeyboard = false;
}
}
if(Input.GetKeyDown(KeyCode.DownArrow)) {
//code logic
useKeyboard = true;
}
It WORKSSS.....!!!! Thank you thank you thank you... (^_^)
As much as I welcome your enthusiasm, I'm not sure what adding another answer, essentially saying the same as the two existing answers does. It's also not terribly well formatted :o/
I acutally added this answer after I got the comment from GC1983... but it got approved now.. after all our discussions.. sorry for that.. !
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Can someone explain this?? KeyCode Error 1 Answer
Disable child with name of a specific object 1 Answer
Unity will not run when I assign an animation to a keystroke 0 Answers
Deactivate Key. 2 Answers