Do something as long as tapping fast

For a mobile game, I would like to make my character do something as long as I tap fast. Does anybody know how I could do that? Should I always check the time between touchphase.began and touchphase.ended or is there a simpler way? Thanks!

You are on the right track. How simple or complex the code needs to be will depend on your restrictions. If you don’t care about how the taps are entered, then the code could be simple. For example this includes allowing the user to drum multiple fingers to enter the taps. If so, then each time you see a TouchPhase.Begin, set a timestamp to Time.time. You continue whatever activity you want as long as Time.time - timestamp is less than some threshold.

Of course if you want to restrict it to a single finger tap, you’ll need something more complex.

Have a timer.

In Update do something like this

timer--; //timer is private int (don't mistake with General int ;)) declared in class' body

if (timer>0) {doSomething();}

When user is pressing button or tap (You NEED to do this with one-time function, like Input.GetButtonDown, when function is fired as long as you hold button, that won’t work properly), just set timer to some value you want maximum delay (in frames) be between taps.

E.g.:

if (Input.GetButtonDown("tap")){
  timer = 30;
}