In this section, we'll see how to script our project:
- We will first make sure to utilize Unity Engine, as we need access to MonoBehaviour:
using UnityEngine;
- 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
{
- 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;
- 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");
}
- 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();
- We need to loop through to check on our input via touch events:
for (int i = 0; i < Input.touchCount; ++i)
- 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)
{
- Now, we will construct a ray from the current touch coordinates on the screen:
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
- We need to do a check to see if the raycast hits anything:
if (Physics.Raycast(ray, out hit))
- 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);
}
}
- In our Update() method, we call our SetLocation script:
private void Update()
{
SetLocation();
}
}
- 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.
- Open the Destroy Target Location script.
- We will need the UnityEngine namespace as usual, since we want to inherit from MonoBehaviour:
using UnityEngine;
using System.Collections;
- 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
{
- We create an OnCollisionEnter method with the parameters of Collision col:
void OnCollisionEnter (Collision col)
{
- 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")
{
- 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.