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:

  1. To give the player the ability to shoot, we need to first create a new script and call it TouchShoot.
  2. 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;
  3. The Update function is the only function in this script. It starts with a loop. The Input class provides us with the touchCount 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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.