Layer Collision Matrix + Unity Navigation System



English Below

Hallo Zusammen,

in einem aktuellem kleinen Projekt, einer Tower Defense Idee, wollte ich gern, dass mein Gegner sich gegenseitig nicht blockieren und durcheinander laufen können. Auch war mein Gedanke, das Unity integrierte Navigationssystem zu nutzen, da ich so ein wenig „organischere“ Bewegungen der Modelle habe und ich mich möglichst wenig um ein Waypoint System kümmern muss.

Das hat soweit auch sehr gut funktioniert, bis ich dann die Gegner einfach am Ende nicht sterben hab lassen, sondern ich sehen wollte inwieweit diese sich nun aufeinander schieben bzw. ineinander schieben. Leider haben hier die Standardeinstellungen mir ein wenig die anfängliche Freude über das einfache Navigationssystem genommen, da die Einheiten sich nun auftürmen und sich gegenseitig blockieren.

Nun gut, dies ist verständlich für ein Navigationssystem, doch es muss ja irgendeine Einstellung oder ein Code geben, mit dem man das abschalten kann.
Und siehe da, es gibt die sogenannte „Layer Collision Matrix“, welche dafür genutzt wird, dass Collider bzw. deren Layer sich nicht mit bestimmten Layern kollidieren. Sprich es kann eingestellt, welches Layer mit welchem Layer kollidieren soll und welche mit welchen nicht.
Dies ist genau dafür gedacht, dass sich wie hier gewünscht Objekte ineinander schieben, bzw. sich bei der Bewegung ignorieren und durcheinander laufen.

Diese Einstellungen bedeuten in der Theorie, dass das Gameobject „Enemy“ mit allen Layer kollidieren soll bis auf das Layer „Enemy“. Sprich Gegner sollen mit allem kollidieren nur nicht mit anderen Gegner bzw. sich selbst.
Das sollte also nun alles perfekt funktionieren.
DOCH HALT
Nicht so einfach. Denn der Test hat gezeigt, dass dies nicht der Fall ist und auch durch Code ich nicht die Objekte dazu bringen konnte nicht miteinander zu kollidieren. Um herauszufinden ob dies generell an der Collision Matrix liegt oder ggf. sogar an dem Navigation System, habe ich ein einfaches Waypoint System erstellt und mit dem hat die Collision Matrix gut funktioniert. Also haben wir ein Problem im Zusammenspiel mit der Collision Matrix und dem Unity Navigation System.
Nach einiger Recherche habe ich dann auch Quellen gefunde die dies bestätigt haben, dass Unity’s Navigations System offensichtlich nicht die Collision Matrix verwendet später habe ich dann doch eine Quelle gefunden die dies bestätigt und Lösungunsmöglichkeiten anbietet.

Original Quelle – Vielen DANK an die Autoren und Kommentatoren

Für mich nun die einfachste Lösung war, die „Obstacle Avoidance Quality“ der Nav Mesh Agents auf „None“ zu stellen um den Enemies zu befehlen keinerlei Hindernisse mehr aus dem Weg zu gehen. Damit gehen sie sich nun selbst auch nicht mehr aus dem Weg.

Wichtig: Der große Nachteil mit dieser Lösung ist, dass die Agents nun wirklich keinerlei Hindernisse mehr aus dem Weg gehen. Sprich wenn Ihr „Dynamische“ Hindernisse in eurem Spiel habt und wollt, das eure Agenten sich nicht aus dem Weg gehen, ist dies keine Lösung für euch, auch diese dynamischen Hindernisse ignoriert werden. Wenn Ihr dem Link folgt, könnt Ihr noch weitere Lösungsmöglichkeiten finden.
In meinem Fall habe ich (zumindest aktuell noch) keine anderen Hindernisse und es funktioniert perfekt.

SUCCESS

Solltet Ihr nun weitere Fragen oder Ideen haben, lasst es mich gern wissen. Und vielleicht hat dies dem ein oder anderen geholfen.





Hello all together,

in my current small project, a tower defense game, my plan was that the walking enemies are not colliding with each other and all can of them walk through other enemies. My plan as well was to use the Unity integrated navigation system, because I don’t wanted to spent so much time in a way point system and making sure, that the moving is working properly.

So far, it worked pretty fine, till I started to not kill the enemies at the end of the level and let them „stop“ in the end to see how they are all moving into each other. Unfortunately they didn’t. 🙁
The standard settings of the navigation system totally didn’t pleased me and my beginning happines about that easy solution was totally gone because the units are all stacking up and not moving into each other.

Okay, that kind of standard settings are fine and totally understandable for a navigation system. But there has to be any option or code to turn that behavior off, so that they are not blocking each other.
After a short google session I found something called „Layer Collision Matrix“, which is used for the purpose that colliders / their layers are not colliding with certain other layers. That means that you can configure which layer should collide with which other layer in your game and with which not.
That is totally meant to be used for objects to move into each other instead of pushing each other around and ignore each other.

These settings mean in detail, that the Gameobject „Enemy“ will collide with all other objects except objects on the Layer „Enemy“. That should result in a setup where all the Enemy Objects will collide with everything but each other.
This should now perfectly.
BUT STOP
Not so easy. The test results that this way to go is not what I expected and hoped. I tried to ignore the layer collision via code, but even that doesn’t worked. So whatever I tried, the colliders are still colliding and I couldn’t stop them to do that.
To check why and to define if this behavior has it reason on the collision matrix or even on the navigation system, I created a very simple way point system which totally worked fine with the collision matrix. That means, there is an issue with the collision matrix and the integrated navigation system.
After some further research in the world wide web, I found a source which confirmed my theory that the unity navigation system will obviously not work together with the collision matrix. I also found some more sources and especially one, which also provides some good solutions for this behavior.

Original Source – Thanks a lot to the Authors and Commentators

For me now, the easiest Solution was to change the „Obstacle Avoidance Quality“ of the Nav Mesh Agent to „None“. This commands the Enemies to not avoid any obstacles on their way. That means, that they do not try to avoid each other and they start to stack into each other.

Important: The very big disadvantage of this solution is obviously that the Nav Mesh Agent now total ignores ANY obstacle. So when you want any „dynamic“ obstacles in your game and your „Agents“ should try to avoid these obstacles, then this is totally not the solution for you. The link above provides some more possible solutions, maybe some of them will fit to your requirements.
In my case I don’t want to have (at least till now) any other obstacles in my game and it works perfectly.

SUCCESS

If you have now any questions or further ideas, please don’t hesitate and let me know. And maybe this helped some of you as well.


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.