Unity touch input

Categories: Game, Script, Touch, Unity

My first encounter with Unity touch input didn’t go as I wanted it to.. After some failed attempt and a test scene in Unity, it did started to make sense. And finding out that touch has phases and FingerId’s helped a lot. 😉 So here’s my take on a controller and firing script for my current project.

private shieldFingerId = -1;

// If nothing is touched, the shield controller has been released
// Know I should test for the shieldFingerId to be released, but this works for my purpose.
if (Input.touchCount == 0)
	shieldFingerId = -1;

foreach (Touch touch in Input.touches)
	// Fire
	if(touch.phase == TouchPhase.Began && !withinShieldController)

	// "Grap" shield
	if (touch.phase == TouchPhase.Began && withinShieldController)
		shieldFingerId = touch.fingerId;

	// Move shield
	if (touch.phase == TouchPhase.Moved && touch.fingerId == shieldFingerId)

As you can see, when I got hold of the information about touch.phase and .fingerId, everything turned out to be very simple indeed.

I found the Unity Script Reference pages to be very limited on the subject of touch input, e.g. information about touch.fingerId only says “The unique index for touch.” no examples, or explanation. But by asking Google, and searching the Unity forums, great resource I might add, it finally made sense. 🙂

Working with touch has forced me to use my LG E975 mobile for testing, which is a slow process. :/ I guess I have to figure out how to make Unity Remote work with my phone. If not, development is going to take forever!

Hope the above code makes sense? Now go make games! 🙂

– Henning

  • Thank you for posting the script. This is the only script that I’ve found that provide an example for the fingerId.

    • Wow, totally missed your comment.. :/
      Thanks, the finger Id was totally nonsens for me to start with, but now, for my current project i just use it without thinking to much about it.
      Hope you have used the information to make something great 🙂

      Now, go make games! 😉

Leave a Reply

Your email address will not be published. Required fields are marked *