Scripting the project

In this section, we'll see how to script our project:

  1. We will first make sure to utilize Unity Engine, as we need access to MonoBehaviour:
using UnityEngine;
  1. Our public class will be called TargetLocationController, which is the same name as the script file we named in the Unity Editor. We will also inherit from MonoBehaviour:
public class TargetLocationController : MonoBehaviour
{
  1. We will create a public GameObject called targetObject; this is so that we can drag and drop our prefab onto this object to set a reference to it:
private GameObject targetObject;

  1. We will create a Start() method at this point. We want to find the object in the project with the tag of targetLocation, as we will be creating it upon a touch event instantiating it:
private void Start()
{
targetObject = GameObject.FindGameObjectWithTag("targetLocation");
}
  1. We will need to create a SetLocation method and instantiate a new raycast to utilize the touch event the way we want for reading finger presses on the screen:
private void SetLocation()
{
RaycastHit hit = new RaycastHit();
  1. We need to loop through to check on our input via touch events:
for (int i = 0; i < Input.touchCount; ++i)
  1. We check to see if the touch count is greater than 0, and if our touch phase is moved:
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
  1. Now, we will construct a ray from the current touch coordinates on the screen:
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
  1. We need to do a check to see if the raycast hits anything:
if (Physics.Raycast(ray, out hit))
  1. If the raycast hits anything, then we will create a new instance of our prefab with the position based on our touch event:
Instantiate(targetObject, new Vector3(Input.GetTouch(i).position.x, 4.23f, Input.GetTouch(i).position.y), Quaternion.identity);
}
}

  1. In our Update() method, we call our SetLocation script:
private void Update()
{
SetLocation();
}
}
  1. Now, we just need a simple collision check script to check if the player object collides with the targetlocation object. We want to destroy our targetLocation object if the player object collides with it.
  2. Open the Destroy Target Location script.
  3. We will need the UnityEngine namespace as usual, since we want to inherit from MonoBehaviour:
using UnityEngine;
using System.Collections;
  1. The name of the class is the same as the name we gave the C# script file and inherits from MonoBehaviour, so we can attach it to a game object in the Unity Editor:
public class DestroyTargetLocation: MonoBehaviour
{
  1. We create an OnCollisionEnter method with the parameters of Collision col:
void OnCollisionEnter (Collision col)
{
  1. We will do a simple if check to see if what we are colliding with is the targetLocation object via the object's tag name:
if(col.gameObject.tag == "targetLocation")
{
  1. If the simple if check returns true, then we will destroy the targetLocation object:
Destroy(col.gameObject);
}
}
}

We have everything we need to finalize the project.