Search code examples
c#unity-game-enginecollision-detectiondestroygameobject

Unity - How to destroy my player by collision?


Hiho i simply try to destroy my object when it touches the redcube :) I used the code here https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html , but it wont work. Some ideas?

pla

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerdeath : MonoBehaviour
{

         void OnCollisionEnter(Collision collision)
         {
             foreach (ContactPoint contact in collision.contacts)
             {
                 Debug.DrawRay(contact.point, contact.normal, Color.white);
                 Debug.Log("collision detected");
             }
             if(collision.relativeVelocity.magnitude > 2)
             {

                 Destroy(gameObject);
             }
         }
}

Solution

  •  void OnTriggerEnter(Collider other)
     {
        if(other.gameObject.tag=="deathcube")
         Destroy(gameObject);  
     }
    

    this worked fine :) but I have heard that its more performance heavy then OnCollisionEnter.