Unity-Arduino how to simulate GetKey() using Arduino as input hardware

I just got started with animations and I would like to make an animation play in loop while a button is pressed, the thing that scares me is that I will probably have a problem in playing the animation using GetKey as I think it seem to work with the hardware integrated in the computer (keyboard/mouse), while my input come form a connected Arduino Mega. Basically every time the button on Arduino is pressed I send one (or a series according to the time elapsed while pressing) of input signals to Unity transmitting the number “201”.

Should I calculate the amount of time between an input and the other to let the script distinguish when I am pressing fast or keeping pressed? there are better ways? can you give me a rough direction? Thanks!

Hi,
Have you checked the Arduino forums? That’s probably where to look for how to make an Arduino emulate a keyboard, or any other input device to use as a game controller. And how to make it send characters as if the button was pressed on a real keyboard.

Thanks for the answer! I am not sure I wrote the question properly, I can already obtain a similar functioning to the keyboard, as the input is sent continuously, I just lack the knowledge to understand how the computer or Unity distinguish when the button is pressed many times or kept pressed. For example if it simply check the speed of the signal I think my Arduino input button would already be eligible for being recognised as continuously pressed, because the repeated signals is fast.

Still I don’e know how to use GetKey on something that is not on the keyboard and therefore have not a specific name assigned. Basically yes, the problem can be probably fixed in Arduino (if the button has a similar hardware of a button on the keyboard), at the same time, I think it should be possible to solve such simple issue by code, faking the input to come from one of the keyboard buttons.

in case someone with the same problem is reading this is what I did by now, it may not be elegant but for now it works.
create a float variable to contain the value of Time.time, and update it every time the interested input arrives. Then for each update, subtract to the current time (Time.time) the variable with the time of the last input and if it is more then a decisecond, then assume the button is not kept pressed.

float inputTime

void update ()
{
   if(/*button pressed*/)
   {
   inputTime = Time.time
   //activate animated GameObject or trigger for animation
   }
 
   if (Time.time - inputTime > 0.1)
   {
   deactivate animated GameObject or trigger for animation
   }
}

Believe Unity is polling the keyboard buttons in every Update loop. So if it’s for making a cheat keyboard, everything faster than half the Update fps are probably useless. Though the game may also detect, and take actions against players pressing the fire button at a super human speed.