Time for action – touch to shoot
For making use of the touch inputs, we will need to add a single script to our player's ship:
To give the player the ability to shoot, we need to first create a new script and call it
TouchShoot
.This script begins with a single variable. A
LayerMask
is used to selectively hit objects with a raycast. There are essentially a lot of layers that should be hit. This one will be used to determine what the player can or cannot shoot.public LayerMask touchMask = -1;
The
Update
function is the only function in this script. It starts with a loop. TheInput
class provides us with thetouchCount
value, which is simply a counter for how many fingers are currently touching the device screen.public void Update() { for(int i=0;i<Input.touchCount;i++) {
As we progress through the loop, we use the
Input.GetTouch
function to access information about each touch. This line of code checks the phase of the touch. Every touch has five potential phases: Began, Moved, Stationary...