C# OnCollisionEnter() 在 Unity3D 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18281385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OnCollisionEnter() not working in Unity3D
提问by Temp Id
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
}
But it is not working. I have tried toggling isTrigger
on both the objects.
但它不起作用。我尝试isTrigger
在两个对象上切换。
回答by lukegravitt
Make sure one of them has a non-kinematic rigidbody attached. Taken from the Unity docs:
确保其中一个连接了非运动学刚体。取自 Unity 文档:
When a collision between two Colliders occurs and if at least one of them has a Rigidbody attached, three collision messages are sent out to the objects attached to them. These events can be handled in scripting, and allow you to create unique behaviors with or without making use of the built-in NVIDIA PhysX engine.
当两个对撞机发生碰撞时,如果其中至少一个附加了刚体,则会向附加到它们的对象发送三个碰撞消息。这些事件可以在脚本中处理,并允许您在使用或不使用内置 NVIDIA PhysX 引擎的情况下创建独特的行为。
From here: Unity3D MeshCollider
回答by Matt Baeriswyl
Have you tried using the OnTriggerEnter() class and setting a collider on the object to a trigger?
您是否尝试过使用 OnTriggerEnter() 类并将对象上的碰撞器设置为触发器?
If it doesnt need to tell what objet its colliding with you could do a simple
如果它不需要告诉它与什么物体发生碰撞,你可以做一个简单的
void OnTriggerEnter(){
Destroy(transform.gameObject);
}
Edit:
编辑:
Also I have done OnCollision like this
我也做过这样的 OnCollision
private string hitobject;
void OnCollisionEnter(UnityEngine.Collision hit)
{
hitobject = hit.gameObject.tag;
if(hitobject == "Plane")
{
isgrounded = true;
}
}
None of the objects are triggers and they dont need ridgid bodies to work.
没有一个对象是触发器,它们不需要脊状物体来工作。
回答by Peter de Rivaz
I had the same problem of OnCollisionEnter
not being called and found this question.
我遇到了同样的问题,OnCollisionEnter
没有被调用并发现了这个问题。
For me, the problem was that I was making a 2D game so the answer is to use the OnCollisionEnter2D
function instead.
对我来说,问题是我正在制作 2D 游戏,所以答案是使用该OnCollisionEnter2D
功能。
回答by Ohad Cohen
Have a look at this table
看看这张表
If you want your OnCollisionEnter
to be called make sure:
如果您希望OnCollisionEnter
被调用,请确保:
(a) Both objects have a collider attached.
(a) 两个物体都有一个对撞机。
(b) None of the objects is a trigger collider (this will issue OnTrigger function & not OnCollisionEnter)
(b) 没有一个对象是触发碰撞器(这将发出 OnTrigger 函数而不是 OnCollisionEnter)
(c) One of the objects (doesn't matter which of them) is a rigid, non kinematic & non static object (the second don't have to be a rigid body).
(c) 其中一个物体(与它们中的哪个无关)是刚性的、非运动学和非静态的物体(第二个不必是刚体)。
(d) Due to computational difficulties MeshCollider might have hard times colliding with other mesh collider, use them with caution.
(d) 由于计算困难,MeshCollider 可能很难与其他网格碰撞器发生碰撞,请谨慎使用。
(e) Make sure both the objects are in the same layer.
(e) 确保两个对象都在同一层中。
回答by Karessa Torgerson
I had a similar problem. The box collider wasn't as big as the collision object. Setting the x and z values to 2 units fixed the problem!
我有一个类似的问题。盒子碰撞器没有碰撞对象那么大。将 x 和 z 值设置为 2 个单位即可解决问题!