简体   繁体   English

碰撞检测在Unity 2D中不起作用

[英]Collision detection not working in Unity 2D

I have two 2D game objects. 我有两个2D游戏对象。 They each have a Box Collider 2D and a Rigid Body 2D which is not kinematic. 它们每个都有一个Box Collider 2D和一个不是运动学的刚体2D。 When the game plays, one moves towards the other and collides with it. 当游戏进行时,一个人向另一个移动并与之相撞。

However, I also have the following method in the moving GameObject: 但是,我在移动GameObject中也有以下方法:

void OnCollisionEnter(Collision collision) 
{
    print( "Collided with someone" );
}

The print statement never prints, so presumably the method is never called. print语句永远不会打印,因此可能从未调用过该方法。 Where am I going wrong? 我哪里错了?

Unity has replicated all of the physics methods for 2D with the word "2D" stuck onto the end! Unity已经复制了2D的所有物理方法,并在末尾粘贴了“2D”字样! So for your example, it should be changed to: 因此,对于您的示例,应将其更改为:

void OnCollisionEnter2D(Collision2D collision)

And the same with basically any other 2D physics thing. 和基本上任何其他2D物理事物一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM