Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

unity3d - 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. Under car and track is outOfTrack layer. When a car is on track the collision between car and outOfTrack is detected, so i want to avoid it somehow.
How to avoid collision detecting between car and outOfTrack when car is on track, and detecting it only when car is touching outOfTrack?

question from:https://stackoverflow.com/questions/65886158/how-to-avoid-collision-between-first-and-third-layer-when-second-is-between-them

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Go to Edit->ProjectSettings->Physics. There you have the collision matrix. With the checkboxes you define which layer collides with which.

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...