To add something else for our player to do, as well as to demonstrate some additional input functionality, we'll make it so 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 and utilize 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="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;
// Create a LayerMask that will collide with all possible
// channels
int layerMask = ~0;
// Are we touching an object with a collider?
if (Physics.Raycast(touchRay, out hit, Mathf.Infinity,
layerMask, QueryTriggerInteraction...