- Home /
Why does my sound play multiple times using touchcount?
I have a simple script that checks if there is a finger on the screen, then plays a sound, then adds velocity to make a character jump. Everything works fine except when I run it on the iphone, the sound is played multiple times each time the screen is touched.
void update() { if (Input.touchCount == 1) { AudioSource.PlayClipAtPoint(audioclip); rigidbody2D.velocity = new vector(0, jumpspeed); } }
Answer by Owen-Reynolds · Feb 14, 2014 at 12:37 AM
Look at the scripting docs for Touches (under Input
.) There's an extra parameter that tells if it just started or just ended (like mouseDown and mouseUp, but named begin and end) or is mid-touch. There are examples.
Since you aren't checking, it replays the sound every frame where it feels a finger, more like how`Input.GetKey` works.
You were absolutely right, not only did it fix my sound but also my infinite jumping while holding the screen down. Thank you! For anyone interested this is the script I used:
void Update() { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) { Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition; transform.Translate(-touchDeltaPosition.x speed, -touchDeltaPosition.y speed, 0); }
Your answer
Follow this Question
Related Questions
Problem with the touch control 2 Answers
Input.GetTouch SOMETIMES not registering on iOS 0 Answers
iPhone input multitouch 1 Answer
Managing Multiple Touch Inputs 0 Answers