Multi-Touch Handling on Unity iPhone

November 3rd, 2008

There are a lot of questions on the Unity forums about how to handle multi-touch on iPhone. We’ve done a few experiments with multi-touch–although nothing in a game yet–but here’s one solution. This had a few goals:

  • Avoid iPhoneTouchPhase, which is unreliable (particularly with the remote)
  • Abstract touch tracking logic from game logic
  • Use a minimal amount of code


Multi-Touch Example (Unity iPhone) from Matthew Wegner on Vimeo.

This solution breaks the problem up into two classes, a Tracker and a Manager.  The core of the manager uses a Hashtable to tie tracker objects to their finger IDs.  The logic looks like:

  1. Reset all trackers to “clean” state
  2. Loop through all touch events
    1. If we don’t have a tracker for this finger ID, create one (and assume touch has started)
    2. If we already have a tracker object for this finger ID, tell it to update
    3. Mark tracker as dirty
  3. Any trackers that are still marked clean are assumed to be ended

There is some logic separation here, although it isn’t as clean as it could be.  The code that’s spawning/moving cubes is placed directly into the TouchTracker class.  In production we would strive to split this up, so the grunt work of tracking touches is totally separate from any game logic.  You could have your game logic get notifications of new/ended touches, or you could subclass TouchTracker and set up functions like OnTouchStarted, OnTouchEnded, and OnTouchUpdated.  Unity presents a lot of options for situations like this.

Enjoy the example, and comment if you make any changes or improvements.  This is far from perfect, but I think it’s a good direction to take.

Download Multi-Touch Sample Project

Let’s Get This Party Started!

October 31st, 2008

Welcome, everybody, to the Blurst technology blog!  This will be our oulet for sharing code, tutorials, and lessons learned from the Flashbang game mines.  We’re doing this to give back to the tremendous Unity community and to help out our fellow game developers.

Unity History

Unity is the engine that powers all of the games you see on Blurst.  It’s a fantastic piece of software, and we’re very happy to have found it.  I first discovered Unity back in December 2006.  At the time, we were still using Virtools, but were unhappy with the evolution of the software and the direction of their company.  I was actively researching other engines when I came across a development contest Unity was sponsoring.  It was the perfect opportunity to give Unity a test drive.  I worked with Adam Mechtley (now one of our in-house artists) and created I Hate Clowns.  I was hooked.

It took awhile to come back to Unity full-time, since we still had internal Virtools projects and external contract work.  In September, 2007, we finally shifted gears to making our own games and began hiring to expand the team.  The games we created eventually laid the foundation and vision for Blurst.  Today we have 6 guys and work with Unity on a daily basis.  It’s fantastic.

Lessons Learned

We’ve been using Unity for awhile now.  Next month we’re scheduled to launch our fifth major Unity game, Minotaur China Shop, along with three iPhone titles made with their new iPhone publishing features.  Needless to say, we’ve learned a lot about Unity.  Our goal here is to share scripts, code snippets, and full-fledged tutorials to help other developers accelerate their learning curve.  We’ve solved quite a few problems; it doesn’t make sense for other people to re-invent the wheel.

Topics

Specifically, we’ve done quite a lot with:

  • Physics
  • Shaders
  • Web integration

But we’ll also be sharing more general solutions to organizational challenges any Unity developer will face (editor/library scripts, how we manage messages, etc).

Blog Format

We’ll be posting snippets as we find time.  We’ll probably be quieter as we approach our deadlines, and then very active as we have more time to collect our thoughts and organize code.  We actually ran a very similar blog years ago, in the Virtools days, where we posted experiments along with source code (DevLab is still online, if anyone is curious).

If you guys have questions on anything we post, please post questions in the comments!  If you’d like to contact us directly to suggest a topic or prod us on any particular subject, we can easily be reached at contact@blurst.com.