Wait between Touch Detects in Unity3D New:

Decralte three variables:

 


[sourcecode language='csharp' padlinenumbers='true']
private bool IsCorrectTime = false;
//timeleft user set with himself for testing in unity
public float timeLeft_p = 2.0f;
//for reseting the time.
private float timeLeft;
[/sourcecode]



[sourcecode language='csharp' ]
Void Update()

{

ClickedTime(); //it looks for method and goes to step 2

foreach (Touch touch in Input.touches)

{

if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)

{

//first time IsCorrectTime is false so noting happens

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && IsCorrectTime)

{

RaycastHit2D ray = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);

// RaycastHit2D ray = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

if (ray.collider != null)

{

Debug.DrawLine(Input.mousePosition, Vector3.right);

Debug.Log("Target Position: " + ray.collider.name);

if (tapCount == 1)

{

anim.SetInteger("IsTapCount", tapCount);

tapCount++;

}

else if (tapCount == 2)

{

anim.SetInteger("IsTapCount", tapCount);

tapCount--;

}

ResetTimer();//it goes to step3 and reset the timer

}

}

}

}

}
[/sourcecode]


Step 2:



[sourcecode language='csharp' ]
void ClickedTime()

{

timeLeft -= Time.deltaTime;

if (timeLeft < 0)

{

IsCorrectTime = true;

//here return is important otherwise it goes down and down value from zero

return;

}

}

[/sourcecode]


Step 3:



[sourcecode language='csharp' ]
void ResetTimer()

{

timeLeft = timeLeft_p;

IsCorrectTime = false;

}
[/sourcecode]

Comments