Unity - 如何通过碰撞摧毁我的玩家?

Unity - How to destroy my player by collision?

嗨,我只是想在我的物体接触到红立方体时摧毁它:) 我在这里使用了代码 https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html ,但它不起作用。一些想法?

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);
             }
         }
}
 void OnTriggerEnter(Collider other)
 {
    if(other.gameObject.tag=="deathcube")
     Destroy(gameObject);  
 }

这工作得很好 :) 但我听说它的性能比 OnCollisionEnter 更重。

你可以使用这个:-

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

注意:在Unity中添加我在截图中给出的标签。