- Home /
iOS TouchPhase.Began Detected Multiple Times
I have a buggy iOS program I can't figure out. I've boiled it down to a simple problem, the TouchPhase.Began code is being run twice, even though the user is just touching my object with the code once. I created a small program to show the problem. Basically, I have a big sphere that when the user touches, I want it to register as one touch. For some reason, when the sphere is touched, the counter tracking touches usually counts multiple touches. I've spent hours with no luck. Here's the code in full:
private var hit : RaycastHit;
private var ray : Ray;
var inputCamera : Camera;
private var i:int = 0;
private var hitTag:String;
private var touchId:int;
function FixedUpdate (){
if (Input.touchCount > 0) {
var touch : Touch = Input.touches[0];
ray = inputCamera.ScreenPointToRay(touch.position);
if(Physics.Raycast(ray, hit)){
hitTag = hit.transform.tag;
switch (touch.phase) {
case TouchPhase.Began:
if (hitTag == "orangeBttn") {
i++;
break;
}
}
}
}
}
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 50), "Orange hit " + i);
}
I can send the program if anyone is interested, but basically I just have the code on a sphere, and run it on the iPad (not just the remote). When the user touches the sphere (tagged orangeBttn), variable i should be incremented by 1. Instead, most of the time its incremented by 2. There's work arounds,but can some body tell me why this simple thing isn't working? Thanks in advance.
Answer by unityRookie · Feb 29, 2012 at 02:41 AM
In case anyone else runs across this problem in the future, the problem was I had the TouchPhase event code in the FixedUpdate function, which made it buggy. I put it in the Update function and it solved the problem.
Thanks. Just run into same problem with old Android TF101 getting double touches. Would vote up but can't as not enough rep yet.
How is this still a bug? I'm using Unity 5.6 ( 2017 ), this answer dates from 2012...
Still a bug... doesn't work correct in FixedUpdate. touch.phase events happen multiple times in fixedUpdate...
Your answer
Follow this Question
Related Questions
Decting iOS touch input in Fixed Update 1 Answer
How do I change the texture of a gameobject only while a touch is on it on iOS? 1 Answer
Input.touchCount returning wrong number 0 Answers
Touch count & Raycasting on mobile 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers