babji3
July 15, 2015, 11:37am
1
Hi all,
I need help in this , I write jump function for Single touch, and Double touch for other function,
here the problem is when i double touch the single touch function also executing , here is my code.
void Update ()
{
//
for(int i = 0; i < Input.touchCount; i++)
{
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
if(Input.GetTouch(i).tapCount == 1)
{
if(isGameStart==true)
{
Jump();
_animator.enabled=true;
if (!this._animator.GetCurrentAnimatorStateInfo (0).IsName ("jumping1"))
{
_animator.SetBool("jumping",false);
}
else
{
_animator.SetBool("jumping",true);
}
}
}
if(Input.GetTouch(i).tapCount == 2)
{
print("Double Touch");
StartCoroutine(SlowMotion());
}
}
}
}
Boz0r
July 15, 2015, 11:57am
2
Move the tapcount 2 if-statement above the tapcount 1, and change the tapcount 1 to an else-if.
babji3
July 15, 2015, 12:01pm
3
You mean i have to put Double Tap Function First?
kemar
July 15, 2015, 12:37pm
4
i think it’s better like this
if(Input.GetTouch(i).tapCount == 2)
{
//Double Tap
}
else if(Input.GetTouch(i).tapCount == 1)
{
//Tap
}
babji3
July 16, 2015, 7:21am
6
No this one also not working.
Same problem. Any other solution?
check how many touches you have… I would kinda expect the first tap to make one touch (tapcount 1) and the second tap to make another touch (with tapcount 2). Since you’re looping through all touches you are triggering both functions. One on the first touch, the other on the second. It’s not logically possible for @kemar 's code to execute both branches with the same i value afterall.
babji3
July 16, 2015, 11:33am
8
Ya thats why i post this question here, i dont know how to loop?
if you wrote the code in the OP you already are, the for loop goes through each touch…
babji3
July 16, 2015, 11:57am
10
Ya i know, What is the solution plz, or any other way for single tap and double tap?
Defero
July 16, 2015, 1:17pm
11
Here’s how i would do it, not saying that it’s good, but it could work (probably)
private float doubleTapTimer = 0.3; //How much time we allow for the double tap
void Update ()
{
for(int i = 0; i < Input.touchCount; i++)
{
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
if(Input.GetTouch(i).tapCount == 1)
{
Invoke("singleTapAction", doubleTapTimer);
}
if(Input.GetTouch(i).tapCount == 2)
{
print("Double Touch");
CancelInvoke("singleTapAction");
StartCoroutine(SlowMotion());
}
}
}
}
void singleTapAction(){
if(isGameStart==true)
{
Jump();
_animator.enabled=true;
if (!this._animator.GetCurrentAnimatorStateInfo (0).IsName ("jumping1"))
{
_animator.SetBool("jumping",false);
}
else
{
_animator.SetBool("jumping",true);
}
}
}