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

查看:393
本文介绍了使两个物理对象不碰撞但在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):现在,您可以告诉引擎

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天全站免登陆