Detecting touch on game objects
To add something else for our player to do, as well as to demonstrate some additional input functionality, we’ll ensure that if the player taps an obstacle, it will be destroyed. We will use the following steps to modify our existing code to add this new functionality, utilizing the concept of raycasts:
- 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="screenPos">The position of the touch /// in screen space</param> private static void TouchObjects(Vector2 screenPos) { /* Convert the position into a ray */ Ray touchRay = Camera.main.ScreenPointToRay(screenPos); RaycastHit hit; /* Create a LayerMask that will collide with...