- Home /
How to call Input.GetButtonDown with another code?
I have a gesture recognition software. I want to perform a click by calling Input.GetButtonDown("Fire1") when user waves his hand. Right now I have:
void Update(){
if(waving code){
Debug.Log("user waved");
Input.GetButtonDown("Fire1");
} }
The "user waved" log shows just fine but the click is not performed. What im trying to do is essentially replace a cursor. User can move the mouse and once he waves his hand, whichever button is underneath will be clicked, so I cant tie waving to a determined button either. If anybody can help me with that or suggest an alternative, I would appreciate it.
Answer by RetroGeek46 · Jun 25, 2018 at 06:26 AM
You can try to make your own Input Manager.
You must be checking for "Input.Get..." to get true somewhere else in your code. Declare a static bool, and use that as input.
For example, lets say your character shoots whenever he waves hands, using a function called Shoot. So it could be like
public static bool fire;
void Update() {
if (waving code){
Debug.Log("user waved");
fire = true;
} else fire = false;
if (fire) {
Shoot();
}
}
By declaring it static you can use it in other scripts too.
Edit: Just saw you using GetButtonDown. For that, as soon as Shoot() is called, just set fire to false again. You can have another bool to keep track if function has been called, then don't set fire as true till wave stops or something like that.
Thank you for your feedback. It looks like you are suggesting to put one specific code in if(waving code) statement such as fire = true in your scenario. However I can already do that.
What im trying to do is essentially emulate a cursor click. User can move the mouse and once he waves his hand, whichever button is underneath will be clicked. But Im trying to make it emulate a mouse click rather than a one predefined code. Sorry for confusing you.
Okay, what you can do is call this yourButton.onClick.Invoke( ) method whenever you want to emulate a click. I'd also suggest to add EventTrigger component and use PointerEnter and PointerExit to check which button is currently active. Or maybe currentSelectedGameObject could help.