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++) ...
Get Unity Android Game Development by Example Beginner's Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.