When user enters a region, activate certain events.
First is using a simple physics collider. Setup BOX Collider or Sphere Collider component on the target object. provide a tag for it, this case called "ball". Do remember to set the isTrigger active on the object too.
Add onTriggerEnter function to the movementControl script:
using UnityEngine; using System.Collections; public class movementControl : MonoBehaviour { public float moveSpeed = 10f; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if(Input.GetKey(KeyCode.UpArrow)) transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); if(Input.GetKey(KeyCode.DownArrow)) transform.Translate(Vector3.back * moveSpeed * Time.deltaTime); if(Input.GetKey(KeyCode.LeftArrow)) transform.Translate(Vector3.left * moveSpeed * Time.deltaTime); if(Input.GetKey(KeyCode.RightArrow)) transform.Translate(Vector3.right * moveSpeed * Time.deltaTime); } void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag ("ball")) { other.gameObject.SetActive (false); } } }
another example is to create a trigger region and set it active when user enters the region:
using UnityEngine; using System.Collections; public class triggerEnter : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag ("Player")) { transform.localScale += new Vector3(3f,3f,3f); } } }