To start with, replace line 5 with:
if (Input.GetMouseButtonUp(0)) {
Now you can play with this code in the Editor. For basic use in touch, you can replace this line by:
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Ended)) {
But this line does not handle multiple touches. It does not distinguish between a tap, a press, or a swipe. So how much additional work you need to do will depend on 1) your definition of touch, and 2) how you want to deal with multiple touches.
As for how this code works, each time it detects a touch, it puts the time of that touch in a list. Lines 8 through 12 comb through that list and remove any that are over one second old. The count of the remaining entries in the list will be the touches per second.
Here is your code modified to handle multiple touches, but it does not distinguish between tap, press, swipe, etc.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
class Example : MonoBehaviour {
private List<float> taps = new List<float> ();
public float tapsPerSecond;
void Update () {
foreach (Touch touch in Input.touches) {
if (touch.phase == TouchPhase.Ended)
taps.Add(Time.timeSinceLevelLoad);
}
for (int i = 0; i < taps.Count; i++) {
if (taps *<= Time.timeSinceLevelLoad-1) {*
-
taps.Remove(i);*
-
}*
-
}*
- tapsPerSecond = taps.Count;*
- }*
}
To distinguish between tap, press, swipe, etc. would require data structures that tracked the fingerId of the touches and saved the position and time of events.
Also your original code has a bug in the for() line where a ‘,’ should be a ‘;’.
You could add a check at the beginning of each Update to see if the time had elapsed... If not enough time had passed, 'if (Input.anyKey)' would detect a key press If enough time had passed, you could then output the number of taps
– Embrocation