使两个物理对象不发生碰撞,但在 Unity 中检测碰撞 [英] Make two physics objects not collide but do detect collisions in Unity

查看:57
本文介绍了使两个物理对象不发生碰撞,但在 Unity 中检测碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Unity 项目,其中有一个由静态对撞机组成的 2D 游戏世界,使几何体与居住在其中的角色保持一致.玩家是一个动态对撞机(具有非运动学刚体).还有一个敌人角色也是一个动态碰撞器.两个角色都像我期望的那样走过地板并撞到墙壁.

I have a Unity project in which there is a 2D game world that consists of static colliders to make the geometry solid to the characters that inhabit it. The player is a dynamic collider (with a non-kinematic rigidbody). There's also an enemy character that is also a dynamic collider. Both characters walk over the floor and bump into walls like I'd expect them to.

我想要实现的是玩家和敌人彼此不牢固,因此他们可以相互移动.我通过将敌人和玩家放在不同的层并设置碰撞矩阵来实现这一点,以便这些层不会相互碰撞.然而,我现在遇到的问题是,我确实想检测敌人和玩家是否遇到了对方.我为敌人角色添加了一个触发器碰撞器,它位于敌人层,这意味着它不会检测到与玩家的碰撞.

What I want to achieve is that the player and enemy are not solid to each other, so they can move through each other. I achieved this by putting the enemy and the player on separate layers and setting the collision matrix so that these layers do not collide with each other. The problem I'm having now, however, is that I do want to detect whether or not the enemy and the player ran into each other. I added a trigger collider to the enemy character, it's on the enemy layer which means it doesn't detect collisions with the player.

我想为敌人制作一个子游戏对象,把它放在玩家层,添加一个刚体并触发碰撞器并用它来检测玩家和敌人之间的碰撞,但感觉如此复杂以至于它让我想知道是否有更优雅的解决方案.

I thought of making a sub-gameobject for the enemy, put it on the player's layer, add a rigidbody and trigger collider to it and use that to detect collisions between the player and the enemy, but it feels so convoluted that it leaves me wondering if there isn't a more elegant solution for this.

推荐答案

编辑 (2020-05-26): 现在你可以告诉引擎忽略两个给定对象之间的碰撞.感谢 Deepscorn 的评论.将保留其余答案不变,并记住这一点.

Edit (2020-05-26): Nowadays you can tell the engine to ignore collisions between two given objects. Kudos to Deepscorn for the comment. Will leave the rest of the answer unchanged, and read with that in mind.

是的,您需要创建一个带有触发器碰撞器的子 GameObject,并将其放在与玩家层交互的层中.

Yes, you need to create a child GameObject, with a trigger collider, and put it in a layer that interacts with the player layer.

不,你不需要在新的GameObject中添加Rigidbody,父级的刚体已经使它成为一个动态碰撞器,所以你会得到OnTrigger 事件.

No, you don't need to add a Rigidbody to the new GameObject, the parent's rigidbody already makes it a dynamic collider, so you will get OnTrigger events.

作为旁注,为了使事情井井有条,如果您创建了敌人的孩子,请不要将其放入玩家层.例如,将来您可能需要禁用播放器与自身的层碰撞.此外,如果您的玩家以这种方式与许多对象交互,我会在玩家而不是敌人上放置一个触发器,在单独的 PlayerTrigger 层上,只是为了简单起见.

As a side note, just to keep things organized, if you create a child of the enemy don't put it in the player layer. For example, in the future you might need to disable the player's layer collision with itself. Furthermore, if your player interacts this way with many objects, I'd put a single trigger on the player instead of the enemies, on a separate PlayerTrigger layer, just to keep things simple.

没有更简单的方法吗?并不真地.你肯定需要玩家和敌人碰撞器之间的非交互,但它们之间也需要某种交互.所以其中之一需要跨越两层,否则整个交互将用一个布尔值来描述.物理引擎一次性处理大量信息,因此您可以设置所需的所有层和碰撞,但在物理循环期间,您无法进一步控制发生的情况.您不能告诉引擎忽略两个对象之间的碰撞.只有 32 层,让它们以严格的方式运行实际上是重优化.如果您担心创建另一个图层的性能,请禁用不需要的图层之间的交互,例如触发图层与地板和墙壁,或者甚至不接触的图层.

Isn't there a simpler way? Not really. You definitely need non-interaction between the player and enemy colliders, but some kind of interaction between them too. So one of them needs to span two layers, or the whole interaction would be described by a single bool. The physics engine processes lots of information in one go, so you can set all the layers and collisions you want, but during the physics loop you have no further control on what happens. You can't tell the engine to ignore collisions between just two objects. Having only 32 layers, and having them behave in the rigid way they do are actually heavy optimizations. If you are concerned about performance for creating another layer, disable interaction between layers you don't need, like the trigger layer and the floor and walls, or layers that don't even touch.

您的替代方案是通过代码来完成这一切,这更不优雅.播放器上的单个子胶囊现在听起来还不错,不是吗?

Your alternative is doing it all by code, which is even less elegant. A single child capsule on the player doesn't sound that bad now, doesn't it?

这篇关于使两个物理对象不发生碰撞,但在 Unity 中检测碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆