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 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.