I’ve been trying to implement a simple double-tap co-routine into my project and am having a bit of trouble. I’m trying to make it so that if you’re zoomed out (using a pinch to zoom) you can double tap to move your camera back to its original state. Here’s the code:
void Start()
{
if (Input.touchSupported && Application.platform != RuntimePlatform.WebGLPlayer)
StartCoroutine(TouchListener());
else
StartCoroutine(MouseListener());
}
private IEnumerator TouchListener()
{
while (enabled)
{ //Run as long as this is activ
if (Input.touchCount == 1 && zoomed == true)
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Ended)
yield return TouchEvent();
yield return null;
}
}
private IEnumerator TouchEvent()
{
//pause a frame so you don't pick up the same mouse down event.
yield return new WaitForEndOfFrame();
float count = 0f;
while (count < doubleClickTimeLimit)
{
if (Input.touchCount == 1 && zoomed == true)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
DoubleClick();
yield break;
}
count += Time.deltaTime; // increment counter by change in time between frames
yield return null; // wait for the next frame
}
else
{
count += Time.deltaTime; // increment counter by change in time between frames
yield return null; // wait for the next frame
}
}
SingleClick();
yield break;
}
It seems as though when I remove my two fingers from the pinch to zoom, it’s registering the last finger removed as my first tap, and my second co-routine’s timer is not breaking, waiting infinite amounts of time for any touch and firing doubleclick… The zoomed variable is being set by my zooming and is working correctly.
Here is the mouse listener co-routine, which is working correctly if anyone is interested:
private IEnumerator MouseListener()
{
while (enabled)
{ //Run as long as this is activ
if (Input.GetMouseButtonDown(0))
yield return MouseClickEvent();
yield return null;
}
}
private IEnumerator MouseClickEvent()
{
//pause a frame so you don't pick up the same mouse down event.
yield return new WaitForEndOfFrame();
if (zoomed == true)
{
float count = 0f;
while (count < doubleClickTimeLimit)
{
if (Input.GetMouseButtonDown(0))
{
DoubleClick();
yield break;
}
count += Time.deltaTime;// increment counter by change in time between frames
yield return null; // wait for the next frame
}
SingleClick();
yield break;
}
}
Any ideas would be greatly appreciated.