Touch count and you, another excercise in what "should" work

Ok, so I’ve got my nice little script to handle user input on the player’s iPhone, all neatly coded so we start, process and cleanup at end. Now, the catch… I don’t seem to be getting a iPhoneInput.touchCount above 0 as the touch ends… (this is Unity v3.0 fyi)…

Anyone have any idea why I wouldn’t have a touch for the end phase?

if so, any good work arounds?

Does anyone know why I thought making games would be fun?

Cheers,

Galen

Touch phases (especially “end”) are not 100% reliable anyway, they often get skipped entirely, especially if your FPS is inconsistent. I don’t know if this is an iOS issue or a Unity one, but I have had nothing but trouble with them. An easy solution is just to keep track of your own touch state manually with some integer variables, which ensures that hiccups in your framerate will never swallow an important event. Something like 0 = not active, 1 = first frame it’s detected, 2 = finger continues to be detected, 3 = first frame its no longer detected, etc. I’m using an array of 5 ints (one per finger) for our 3rd game due to its multi-touch needs - I’d just post my code but its pretty highly specialized at the moment.

BTW, this IS the fun part. Game development is a constant string of problems to solve or work around. :slight_smile:

Hey, thanks MikaMobile,

I thought about trying to handle it manually, but how do I register the events without the touch array? My thought was to store the last touch (since I won’t get a touch at the end) and give the stupid thing a couple of frames worth of doubt, then call the ball. ie, if it’s been 3 frames and no new updates, assume it’s over. Or do you have a better approach?

hehe.

Cheers,

Galen

Using touchCount might work if your game doesn’t need to use multitouch. For Battleheart, I need to keep track of multiple fingers and know exactly which finger is in what state, so I used an array to keep track of my own arbitrary “touch phases” for starting, ending, dragging, etc., using not the index of the touch in the touchCount array, but rather the touch.fingerId to determine which was which. So basically, I iterate through Input.touchCount using Input.GetTouch. For each touch, I check the finger ID, which correlates to a slot in an array of ints that tell me what that finger is up to.

I also use a boolean to determine the very first frame that a finger is released. At the beginning of each update, before any touches are processed, the bool is set to false. It’s only set to true if the finger in question is detected as “down” that frame. So after all of my touches are processed, if that bool is still false, and yet the touch state isn’t zero (it’s ‘inactive’ state), that means the finger was just released after doing something.

It probably sounds a little clunky (and maybe it is) but it works. :slight_smile:

Ok so let me see if I can find a reliable control point, (if you could verify my logic that’d be great :slight_smile: ), which is the most reliable approach:

  1. touchCount is still > 0 even after touching has stopped, so I just need to process the last touch as the end state even if the touchPhase hasn’t changed?

or

  1. touchCount is not accurate, and I should expect that count to drop to 0 without ever giving me any kind of touchPhase?

If I take an approach to handle #2, then it’s sort of a catch-all… basically don’t trust the Input at all beyond starting (I have to rely on it at some point :slight_smile: … you know this sounds very familar, I think Input.Touchxxx worked for me at some point, I wonder if I still have an employee file :wink: ).

For my purposes right now, I just need to track and process a single finger, so i won’t need an array.

Thoughts?

Thanks again,

Galen

#2 is the issue. I use touchCount all the time and it’s accurate to what’s actually happening, it’s just that the touches themselves will sometimes fail to give you an “ended” touch phase, even though the touchcount actually drops.

Thanks for all your help!

I went ahead and implemented my own touch phases, and retain the last touch between update() calls, so right now even if touch count drops to 0, I’ll still force the end phase processing based on the last known touch.

Cheers,

Galen