as well as supporting input from devices
like a mouse and keyboard. Unity also
supports input from touch screens.
Because of the nature of touch input, it
is handled slightly
differently. We still use the input
class as we would for other input
devices. However, since the user may be
making multiple touches at once, they
are stored as an array.
If we want to retrieve a specific touch,
it is efficient to use the get touch
function of the input
class. The get touch function reads in a
single integer and returns a touch from
the array at that index. For example, if
we want to access the first touch in the
array, we can create a touch variable
and set it equal to input.get touch
passing in a zero.
A touch variable is an instance of the
touch strruct. A strruct is very similar
to a class. It is a collection of
variables and functions just the same as
a class. And for this example, they can
be thought of as the same. The
touchstruct has a number of properties
that we can use in different ways
depending on how we want the touches to
affect our game.
The finger ID property is an identifier
that is unique to that specific touch
for its
lifetime. Using this, you can track the
actions of a specific touch. Once the
touch is no longer in the array, the
touch has finished and other touches can
have this
ID. The position property is the
position of the touch on the screen.
This is a vector 2 measured in screen
space. Screen space is measured in
pixels from the bottom left corner. That
means that axes are between zero in the
lower left corner and the screen's width
in pixels on the right and screen's
height in pixels at the top. The delta
position property is a vector 2 that is
the difference between the touch's
position last frame to its position this
frame. This is very useful for any
system where you need to know the
movement direction of a touch.
In addition to the delta position, the
touch strct also has a delta time
property. This is the time since there
was an update of the touch. Generally,
touches are detected in the update loop.
Under these circumstances, the delta
time property would be the same as the
time.elt time. Each touch has a tap
count property. This is a measure of how
many taps are made in quick succession.
When a tap is recorded, the finger ID is
maintained. However, if you have
multiple fingers tapping at once, then
this will be treated as the same finger
jumping around the
screen. The last property is the touch's
phase. This is an enumeration called
touch phase. The possible values for the
phase are began, cancelled, ended,
moved, and stationary.
The phase began will be returned on the
first frame of the touch. Stationary is
when the touch isn't moving and moved is
when the touch has changed position on
the
screen. Ended is returned on the last
frame of the
touch. The touch will have a phase of
canceled when the device can't handle
the input of the screen. This might be
where there are more touches than the
device supports or a large surface
presses on the screen.
We can also retrieve the entire array by
creating an array of touch objects and
setting it equal to the touches property
of the input class. Now that we have our
touch array, generally it is a good idea
to iterate through them. By doing so, we
can treat each touch individually based
on its properties. To do this, we will
use a for loop. In order to loop the
correct number of times, we could use
the length property of the touches
array. However, it is again more
efficient to use the touch count
property of the input class. From here,
we can treat each touch individually.
However, this might not be the way you
want to treat touches in every
circumstance. For example, there are
times where you might want to write code
that requires a specific number of
touches for complex gestures.
For more examples of touch-based input,
see the assignments linked below.