本文介绍了OnCollisionEnter()在Unity3D中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有网格碰撞器的对象和一个带有球形碰撞器的预制体.如果两个碰撞,我希望销毁预制实例.
I have an object with a mesh collider and a prefab with sphere collider. I want the instance of the prefab to be destroyed if the two collide.
我在脚本中写了以下内容:
I wrote the following in a script:
private void OnCollisionEnter(Collision c)
{
if (c == target)
Destroy(transform.gameObject);
print("something"); // Doesn't get printed
}
但是它不起作用.我尝试在两个对象上切换isTrigger
.
But it is not working. I have tried toggling isTrigger
on both the objects.
推荐答案
我也遇到了相同的问题,即未调用OnCollisionEnter
并发现了这个问题.
I had the same problem of OnCollisionEnter
not being called and found this question.
对我来说,问题是我在制作2D游戏,所以答案是改为使用OnCollisionEnter2D
函数.
For me, the problem was that I was making a 2D game so the answer is to use the OnCollisionEnter2D
function instead.
这篇关于OnCollisionEnter()在Unity3D中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!