Unity touch input

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

2 Responses to Unity touch input

  1. One says:

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

    • Henning says:

      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 *


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /var/www/incd021.com/public_html/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048