简体   繁体   English

当第二层在第一层和第三层之间时,如何避免它们之间的碰撞? 统一二维 C#

[英]how to avoid collision between first and third layer when second is between them? Unity 2D C#

I have 3 layers: car , track and outOfTrack layer.我有 3 层: cartrackoutOfTrack层。 Under car and track is outOfTrack layer.cartrack下是outOfTrack层。 When a car is on track the collision between car and outOfTrack is detected, so i want to avoid it somehow.汽车轨道上时,会检测到汽车outOfTrack之间的碰撞,所以我想以某种方式避免它。
How to avoid collision detecting between car and outOfTrack when car is on track , and detecting it only when car is touching outOfTrack ?如何避免在汽车轨道上时检测汽车outOfTrack之间的碰撞,并且仅在汽车接触outOfTrack时才检测它?

Go to Edit->ProjectSettings->Physics. Go 到编辑->项目设置->物理。 There you have the collision matrix.那里有碰撞矩阵。 With the checkboxes you define which layer collides with which.使用复选框,您可以定义哪个图层与哪个图层发生碰撞。

在此处输入图像描述

Edit: The collision matrix isn't supposed to change all existing contacts in realtime.编辑:碰撞矩阵不应该实时更改所有现有联系人。 If we were to do this, we'd have to re-evaluate every single contact in the world which could be very expensive and is easily abused.如果我们要这样做,我们将不得不重新评估世界上的每一个联系人,这可能非常昂贵并且很容易被滥用。 Check this .检查这个 So you can try to change the collision matrix with code, you can probably do so, however I would suggest to handle the collisions thinking of them in terms of space , not time .因此,您可以尝试使用代码更改碰撞矩阵,您可能可以这样做,但是我建议根据空间而不是时间来处理考虑它们的碰撞。

By this I mean that you try to detect the collision not at the time when your car is out of track, but, with the corresponding layers and colliders, detect the collision where its out of track.我的意思是,您不是在汽车偏离轨道尝试检测碰撞,而是通过相应的图层和碰撞器,其偏离轨道时检测碰撞。

If you are still into changing the collision matrix in runtime you can try check out the docs如果您仍然想在运行时更改碰撞矩阵,您可以尝试查看文档

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

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