简体   繁体   English

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

[英]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.嗨,我只是尝试在接触 redcube 时销毁我的对象 :) 我在这里使用了代码https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html ,但它不起作用。 Some ideas?一些想法?

解放军

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

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.注意:在 Unity 中添加我在屏幕截图中给出的标签。

在此处输入图片说明

 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.这很好用 :) 但我听说它的性能比 OnCollisionEnter 更重。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM