Detecting touch on game objects

It's great to know that our regular input is working, but you may want to check whether a game object in our scene has been touched so that we can react to it. In our case, to add something else for our player to do, we'll make it so that if the player taps an obstacle, it will be destroyed.

  1. In the PlayerBehaviour script, add the following new function:
/// <summary> /// Will determine if we are touching a game object and if so  /// call events for it /// </summary> /// <param name="touch">Our touch event</param> private static void TouchObjects(Touch touch) {     // Convert the position into a ray     Ray touchRay = Camera.main.ScreenPointToRay(touch.position);      RaycastHit hit;   // Are we touching an object with ...

Get Unity 2017 Mobile Game Development now with O’Reilly online learning.

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