0
votes

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);
             }
         }
}
2
Have you tried giving tag to the redCube. And then use it in OnCollisionEnter? - gameDev_Unity
it works but just with ontriggerenter - Delidragon

2 Answers

2
votes

You can use this:-

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);
             }

             if(collision.gameObject.tag=="deathcube")
             {
               Destroy(gameObject); 
             } 
         }
}

Note: Add The Tag in Unity which I give in Screenshot.

enter image description here

0
votes
 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.