【发布时间】:2019-05-19 20:56:16
【问题描述】:
我正在制作某种进化模拟器游戏。我有一个脚本,当一个生物的 CapsuleCollider 触发 OnTriggerEnter() 时,它应该销毁它所连接的 GameObject。
我有一个问题,即使 Creature 的对撞机甚至没有靠近食物,它仍然会破坏 GameObject。
我的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FoodEat : MonoBehaviour
{
public GameObject FoodGO;
public Rigidbody FoodRB;
private void OnTriggerEnter(Collider Creature)
{
Destroy(FoodGO);
}
void Start()
{
FoodRB = GetComponent<Rigidbody>();
FoodGO = FoodRB.gameObject;
}
void Update()
{
Rigidbody[] allRigidBodies = (Rigidbody[])FindObjectsOfType(typeof(Rigidbody));
foreach (Rigidbody body in allRigidBodies)
{
if (body.gameObject.layer == 10)
{
OnTriggerEnter(body.gameObject.GetComponent<CapsuleCollider>());
}
}
}
}
【问题讨论】: