【发布时间】:2022-01-21 06:02:43
【问题描述】:
我对 OnTriggerEnter 的工作方式或 Unity 中的 Colliders 的工作方式感到非常困惑。我正在尝试添加一个碰撞测试,如果子弹击中一个物体,它会播放一个粒子系统并调试一些东西,但它不起作用,我很困惑为什么。请帮忙。
这是我的代码:
public class bulletScript : MonoBehaviour {
public ParticleSystem explosion;
public float speed = 20f;
public Rigidbody rb;
bool canDieSoon;
public float timeToDie;
void Start()
{
canDieSoon = true;
}
// Start is called before the first frame update
void Update()
{
rb.velocity = transform.forward * speed;
if(canDieSoon == true)
{
Invoke(nameof(killBullet), timeToDie);
canDieSoon = false;
}
}
private void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "ground")
{
Debug.Log("hit ground");
explosion.Play();
}
if (collision.gameObject.tag == "robot")
{
Debug.Log("hit ground");
explosion.Play();
}
if (collision.gameObject.tag == "gameObjects")
{
Debug.Log("hit ground");
explosion.Play();
}
if (collision.gameObject.tag == "walls")
{
Debug.Log("hit ground");
explosion.Play();
}
if (collision.gameObject.tag == "doors")
{
Debug.Log("hit ground");
explosion.Play();
}
}
void killBullet()
{
Destroy(gameObject);
}
}
【问题讨论】: