We need a method that can interpret a touch on the 2D surface of the screen and figure out which boxes we are hitting using an imaginary ray traveling from the camera toward the scene we are looking at. This method is called DetermineHit(). Let's set this up by going through the following steps:
- In the WhackABox project, open the Game.cs file.
- Add the DetermineHit() method anywhere in the class, as shown in the following code block:
private void DetermineHit(float x, float y)
{
var cameraRay = camera.GetScreenRay(x, y);
var result = scene.GetComponent<Octree>
().RaycastSingle(cameraRay);
if (result?.Node?.Name?.StartsWith("Box") == true)
{
var node = result?.Node;
if (node.Components.OfType<Death>().Any())
{
return;
}
node.CreateComponent<Death>();
}
}
The x and y parameters that are passed into the method range from 0 to 1, where 0 represents the left edge or...