- Home /
Question by
LochandLoad · Nov 27, 2013 at 02:41 AM ·
destroy objectcollisiondetectionkill player
How can I destroy my Player when it touches another object?
I am making a 2D game and I need my player to die when it touches another object. How can I do this?!?!
Comment
Give the other object one of the 2d colliders, set it as trigger.
$$anonymous$$ake a script so that when the player enters the collider, the object destroys the player by using Destroy(hit.collider.gameObject);
Something like that
Answer by belvita · Nov 29, 2013 at 12:02 PM
using UnityEngine; using System.Collections;
public class SandCubeistrigger : MonoBehaviour {
public static Vector3 touched_object;
public static bool Colexists=false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "theobjecttagnameuwannatouch")
{
Colexists=true;
Destroy(this.gameObject);
}
}
void OnTriggerExit(Collider other) {
Colexists=false;
//Debug.Log("Collission does not exist");
}
void OnTriggerStay(Collider other) {
if (other.gameObject.tag == "sandcube")
{
Colexists=true;
}
}