- Home /
Problem With touch and double touch
Hi everyone. I made a script that should destroy a pointed gameobject with one touch and do the slow motion with a double touch. Now I have made first the destroy on touch script and it worked well. Then I tried add the one touch and double touch script and it doesn't work. I get an error playing: Index out of bounds. The slow motion effect works but it doesn't stop after 3 seconds.The destroy on touch doesn't work at all. Here's my code and thanks for your time:
void Update ()
{
#if UNITY_ANDROID || UNITY_IPHONE
if (Input.touchCount > 0)
{
touchDuration += Time.deltaTime;
touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Ended && touchDuration < 0.2f)
StartCoroutine("singleOrDouble");
}
else
touchDuration = 0.0f;
#endif
}
IEnumerator singleOrDouble(){
yield return new WaitForSeconds(0.3f);
if (touch.tapCount == 1) {
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint ((Input.GetTouch (0).position)), Vector2.zero);
if (hit.collider != null) {
Destroy (hit.transform.gameObject);
}
}
else if(touch.tapCount == 2){
if (Time.timeScale == 1.0f)
Time.timeScale = 0.3f;
}
if (Time.timeScale == 0.3f) {
currentBulletTimer += Time.deltaTime;
}
if ( currentBulletTimer > bulletTimeAllowed ) {
currentBulletTimer = 0f;
Time.timeScale = 1.0f;
}
}
Thats because by the time you do the Raycast and trying to GetTouch(0), your finger might already be off of the screen and therefore there is no touch 0. read the position from touch, which you already saved.
Overall tip: Try to differentiate between single or double touch first, THEN run a coroutine for the single, or for the double touch accordingly.
So I should make two coroutine, one for the single touch and one for the double touch, that are called by a if statement that look for the number of tap?
Your answer
Follow this Question
Related Questions
Press Back Button Twice to Quit Game 0 Answers
iOs/Android Development 2 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Android touch 3d Object event 1 Answer
Cannot touch cloned prefabs 0 Answers