In this final video for our series
we're going to set our game up to use
touch screen controls.
To do this we're going to switch our build
target from the default standalone build target
to iOS, and we're going to add some code
to our Player script to handle touch screen input.
Let's go to File
Build Settings
Make sure that your main scene
is added to the scenes in the build
and then we're going to click iOS
and choose Switch Platform.
With that done we can close our build settings.
And we're going to open our Player script in Monodevelop.
In the Player script the first thing that we're going to add
is a private vector2 called touchOrigin.
We're going to use touchOrigin to record
the place where the player's finger
started touching our touch screen.
We're going to initialise it to -Vector2.one
which is going to be a position off the screen.
This means that our conditional that we're going to use
to check and see if there has been any touch input
will initially evaluate to false
until there actually is a touch,
which changes touchOrigin.
In our update function we're going to
add the mobile input code.
Now we're actually going to add some
platform-specific code in here which is going to check
what platform the game is running on and
use appropriate input controls
so we're initially going to check if we're
running in a standalone build of our application
or we're running in a web player
in which case we're going to want to continue to use
the keyboard controls that we've been using thus far.
If we're not running in a standalone build
or a web player we're going to use
else to encompass all other build targets
including iOS, Android, Windows Phone 8, etcetera.
Next we're going to check if
input.touchCount is greater than 0,
meaning the input system has registered
1 or more touches.
If that's true we're going to store the
first touch detected in a variable of the type
Touch called myTouch.
In this case we're just going to grab the
first touch and ignore all other touches
because the game is going to support
a single finger swiping in the cardinal directions.
Next we're going to check the phase of that touch
and make sure that it's equal to
Began by checking it's TouchPhase
so that we can determine that this is
the beginning of a touch on the screen.
If that's true we're going to set our
variable touchOrigin to equal
the position of myTouch.
If the TouchPhase is not Began
we're going to check if the TouchPhase
is equal to Ended and if the TouchOrigin
is greater than or equal to 0.
Since we initialised TouchOrigin
to -1 we can now check if we had a touch that
ended meaning the finger lifted off the screen,
and if the TouchOrigin.X
is greater than or equal to 0
meaning the touch is inside the bounds of the screen
and has changed from the value that we initialised
it to when we declared it, and that it has ended.
If that is the case we're going to declare
a vector2 called TouchEnd and set it
to equal myTouch.position.
Next we're going to calculate the difference
between the beginning and end of the
touch on the X axis,
We're going to declare a float called X
and set it to equal touchEnd.x - TouchOrigin.x
By doing this we get the difference between
the two touches on the X axis
and therefore we can get a direction to move in.
We're going to do the same on the Y.
And then we're going to set touchOrigin.x
to -1 so that our conditional
doesn't repeatedly evaluate to true.
Because touch controls are not always
in a perfectly straight line we're going
to have to take a guess at the user's
intent with their touch, and so what we're going to do
is we're going to figure out if the touch was
generally more horizontal
or generally more vertical in a given direction.
We're going to do this using mathf.absolute
and checking if X was greater than Y.
If X is greater than Y
we're then going to check if X
is greater than 0
to determine whether it's a positive or negative
number and thereby what direction
the user is trying to swipe in.
We are then going to set horizontal to 1
or -1 accordingly.
If the difference is not greater along the X axis
then what we're going to do is we're going to set
vertical to equal 1 or -1.
We're going to add an endif statement
at the end of our mobile code.
Now to tie this back in to the code that we wrote
earlier when we wrote the original player movement code
the values that we were previously getting
using Input.GetAxisRaw from the input manager
from the keyboard, which we stored in to
horizontal and vertical respectively,
are instead being set in our touch screen code using swipes.
If we've determined that they've changed
in this frame, meaning that they're no longer equal to 0,
the values they were initialised with
we're going to call AttemptMove
and parse in horizontal and vertical.
Now that our touch code is completed you'll be
able to test the game using Unity Remote
or build to your touch device of choice.
It's worth noting that if you want to continue
testing with the keyboard in the editor
you'll need to either change the build type
back to standalone, or you can add
an additional condition to our
first if statement, if Unity_Editor.
And there we have it. If you want to give this a test
you can download the Unity Remote app
for your mobile or tablet device
and hook your device up to
the computer you're running Unity on with USB cable
and you should be able to test your game in the editor.
Thanks so much for watching
and I hope this was helpful for you.
Subtitles by the Amara.org community