英文:
Unity: OnTriggerEnter2D not firing
问题
我有一个预制体,是一个红色矩形,上面有一个设置为触发器的 BoxCollider2D。我还有一个由鼠标控制的玩家,它也有一个 BoxCollider2D(没有设置为触发器)。当玩家与它接触时,应该显示游戏结束画面,但它没有这样做。
英文:
I have a prefab for a red rectangle that has a BoxCollider2D set to trigger on it. I also have the player, controlled by the mouse, and it also has a BoxCollider2D (not set to trigger). When the player comes in contact, it should show a game over screen, but it does not do that.
答案1
得分: 1
在您提供的评论中,您的Rigidbody2D上的Simulated
属性已关闭。没有这个属性,Rigidbody2D将不参与物理模拟,所以碰撞器基本上不起作用。
所以只需启用Simulated
属性。
Unity 2D物理使用Box2d。这个Simulated
属性在Box2d文档中被称为激活。
您可能希望创建一个身体,但不参与碰撞或动力学。这种状态类似于休眠,但不会被其他身体唤醒,身体的夹具也不会放在广义相位中。这意味着身体不会参与碰撞、射线投射等等。
英文:
In the image you provided in the comments, the Simulated
property on your Rigidbody2D was turned off. Without this, the Rigidbody2D does not participate in the physics simulation, so the collider basically doesn't work.
So just enable the Simulated
property.
Unity 2D physics uses Box2d. This Simulated
property is referred to as activation in Box2d documentation
> You may wish a body to be created but not participate in collision or dynamics. This state is similar to sleeping except the body will not be woken by other bodies and the body's fixtures will not be placed in the broad-phase. This means the body will not participate in collisions, ray casts, etc.
答案2
得分: 0
在Unity中检测碰撞,需要其中两个对象之一附加Rigidbody2D。如果游戏是2D的话,用于检测碰撞的函数称为"OnTriggerEnter2D",但在3D中,它只是"OnTriggerEnter"。
如果在将Rigidbody2D添加到其中一个对象后仍然无法正常工作,您能否请提供您的脚本以及可能的Unity项目截图?
英文:
To detect a collision in Unity, one of the two objects needs to have a Rigidbody2D attached to it. If the game is in 2D, the function to detect it is called "OnTriggerEnter2D", but in 3D, it's just "OnTriggerEnter".
If it still doesn't work after adding a Rigidbody2D to one of the objects, could you please show your scripts and maybe some screenshots of your Unity project?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论