- Home /
How to add more than 31 Layers (maximum reached)
Hi, I am making a trafficsystem. Every Car has the same Script, it says react when ther other GameObject has the tag "Waypoint". Each car should only react to the boxcollider on its own path. So I added Layers, sothat CarA react only to PathA, CarB to PathB. Now I am running out of Layers... What can I do?
Answer by Bunny83 · Jun 05, 2019 at 07:17 PM
Layers can not be added or removed. The layers are implemented as a 32 bit bitmask. Therefore there are always 32 layers, no more or less. Like @BradyIrv said you could use IgnoreCollision, however that's probably overkill when you actually want to have 32+ individual path and you have to ignore all colliders from all other paths. If one path has just 10 waypoints and you have 32 paths, each path need to exclude 31*10 waypoints (310). So you would need to call (without handling duplicate pairs) about 9920 IgnoreCollision calls. Though the complexity grows by n² so not very feasible.
It would make more sense to just use one layer for the waypoints and check which waypoint you collide with. So you can mark the waypoints belonging to a particular car in a custom script on the waypoint.
Though waypoint systems usually are easier to implement by not using colliders at all- When each car knows its list of waypoint it can simply check the distance to the next waypoint and when close enough switch to the next one. This has the least processing overhead.
If you have really a lot of cars you can do the distance checking in a coroutine and using an adaptive techique based on the remaining distance / time to the next waypoint. So for example since you know how fast a car can move at most you can simply divide the remaining distance to the target waypoint by that speed. This gives you the time it takes until you reach the target. If the time is greater than 0.5 seconds you could simply use yield return newWaitForSeconds(remainingTime / 3);
So you don't check the distance that often once you get relatively close you may want to switch to yield return null;
to check every frame.
Answer by BradyIrv · Jun 05, 2019 at 06:43 PM
Have you tried instead keeping a collection of the paths/cars and using a method calling Physics.IgnoreCollision? For example you could make a dictionary for pathA's collider, then add any car to the dictionary that you would like. And when you do so, ignore collision with all other paths colliders.
Then you don't have to use layers, and you can create as many paths or cars as you wish.