- Home /
Question by
Emanuel19861 · Aug 13, 2017 at 10:11 PM ·
inputmobiletouchgestures
How to trigger sound only when both fingers lifted off screen
if (Input.GetKeyDown("space") || Input.touchCount == 2)
{
ClearAll(StrokesContainer);
Touch touch1 = Input.GetTouch(0);
Touch touch2 = Input.GetTouch(0);
if ((touch1.phase == TouchPhase.Ended) && (touch2.phase == TouchPhase.Ended))
{
audioElement.Play();
}
}
This the current code I have. Basically I want to clear the screen when the user touches it with two fingers simultaneously, and play a sound clip at the same time. However if the user keeps both their fingers on the screen the sound should still play only once.
Does anyone have any idea how to achieve this effect?
Comment
Your problem is sound plays more than one time? In this case you can set a new bool to check it;
bool soundPlayed;
if (Input.Get$$anonymous$$eyDown("space") || Input.touchCount == 2)
{
ClearAll(StrokesContainer);
Touch touch1 = Input.GetTouch(0);
Touch touch2 = Input.GetTouch(0);
if ((touch1.phase == TouchPhase.Ended) && (touch2.phase == TouchPhase.Ended) && !soundPlayed)
{
audioElement.Play();
soundPlayed = true;
}
}
Now you only need set soundPlayed to false, when you want, when user not touch with two fingers.
Best Answer
Answer by Emanuel19861 · Aug 14, 2017 at 03:25 AM
Yes! That was it! So simple, so elegant! THANK YOU!
Also I used the .isPlaying
attribute to check directly without creating my own bool and got rid of TouchPhase
altogether.
if (Input.GetKeyDown("space") || (Input.touchCount == 2 && !audioElement.isPlaying))
{
ClearAll(StrokesContainer);
audioElement.Play();
}