什么都没有被破坏?我有一个敌人,它是一个具有对撞器和刚体的球体,以及一个具有该刚体的立方体。我尝试过在任一触发器上启用触发器,但重力失灵,并且没有任何破坏!请帮忙!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyOnCollision : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("Player"))
{
Destroy(collision.gameObject);
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Destroy(other.gameObject);
}
}
}
答案 0 :(得分:0)
您需要将脚本添加到播放器和敌人GameObject中,而不是将它们放入一个单独的脚本中。
因为OnTriggerEnter和OnCollisionEnter在碰撞或进入触发器的对象上被调用
您不能仅制作处理所有冲突的通用脚本,因为该脚本或对象不会与任何对象发生冲突。因此,这些函数永远不会被调用,因此不会破坏任何东西。
更改播放器脚本(添加):
// The Enemey needs to have IsTrigger enabled
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Destroy(other.gameObject);
}
}
更改的敌人脚本(添加):
// Player needs to have IsTrigger disabled
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("Player"))
{
Destroy(collision.gameObject);
}
}