This might be a dumb question, but I was under the impression that we could only have 32 layers in our games because Unity was 32-bit. Now that Unity 5.0 will be 64-bit, does that mean we can have 64 layers in our games?
I may not be understanding it correctly or I could be wrong about the layer limit too. Thanks for any info.
What do people use layers for? Isnât their intended use to reduce the number of items the physics system has to interact with? I canât imagine using anywhere near 30 layers at once :o
Ya thatâs pretty much the reason I use them Tomnnn. It also helps with determining what things can be âRaycastedâ against as well. Iâm building an RTS though, and Iâm getting close to 32 layers right now. It didnât seem possible to me either, but Iâm there right now
Itâs pretty big landon. Have you done an RTS before?
Basically, itâs the fact that some units can interact with some things that others cannot. Some units can use terrain that others cannot. Some things need to collide with other things, which cannot collide with other units, but can collide with these objectsâŚetc etc etc.
Resources, buildings, terrain, units, âŚthe list goes on. Not to mention, one main portion of my gamesâ mechanics is heavily based on physics, on top of it being an RTS to begin with.
AnywaysâŚyes, Iâve filled the layers and feel like having even more would give me some breathing room. Iâm having to make use of existing layers that fulfill most of my requirements, but canât fulfill all of them right now.
The good news is, Iâm close to finishing most of the features in my game, so I should be somewhere around 32 layersâŚhaving a few extra would be nice though.
How about taking advantage of monobehavior components? I imagine thereâs a few things youâre using layers for because you need some kind of uniform property system, yes? If thatâs the case, have a property component with a handful of flags in it.
I donât recall when I needed this, but it was better than having a million layers or tags.
âedit
I can see in another reply of yours that it is indeed the case! Not for the physics stuff, but for the interaction stuff. You can make a property script that every unit has on it that you can use to determine if said unit can interact with whatever.
I think if youâre good with bitwise operations and enums, you can make a very efficient flag system instead of a handful of bools. LikeâŚ
9th bit = 1 means air unit
8th bit = 1 means the unit is flying
So your property bit would look like 1001 and mean those things.
Wouldnât it be nice if you could set a property on some units to determine what they can do instead of layers? Like no matter what kind of enemy it is, have some â.isFlyingâ variable that you can access? Make a property script, then. Make it nothing but a few boolean definitions like
public bool isFlying;
public bool canUseRoadTypeB;
public bool isOnFire;
and other stuff you might otherwise need layers for. Then you can do something likeâŚ
and so on. I donât know how performant that is because Iâve only used it in small projects. I think the bit flags would perform best. But itâll remove your layer limits ^-^
Hmmm, Iâm still trying to grasp how that would be useful though. I guess, what do you mean by âinteractâ here as well? Because to me, itâs all about knowing if they can âcollideâ or be âdetectedâ by physics functions and Unityâs physics API.
Most of my physics needs are for âdetectionâ purposes â as my current âdetectionâ system is physically based. So I guess my question is how could coming up with a new flag system tie in with Unityâs physics system?
I also could have up to 4 different âteamsâ, and to save on performance, each team can only detect âenemyâ teamsâ units â excluding their own team. So each team needs their own layer. I think the key for me, is performance. By only allowing units on any given team to not have to detect certain things, it saves big on performance.
Iâm not saying I donât need the property system you stated, but simply that I donât understand currently how it would work for things like detecting enemy team units, detecting air vs. ground objects, detecting resource locations, obstacles, buildings, etcâŚ
I think the biggest culprit here is the Physics.OverlapSphere function makes an array no matter what, and not only that, it creates an entirely new array each time. So, itâs terrible for performance when detecting large numbers of units in my game. So to try to compensate for the terrible performance, I had to create more layers to cut down on the number of objects detected by the function each time it is called.
I could leave a lot of things on one layer and use flags/properties to determine what it isâŚbut then the performance in detecting them initially would be catastrophic.
** Unfortunately I have not yet built a detection system based on quad-trees or oct-trees or some form of spatial partitioning yet â although I know for most rts games this is probably how itâs done. For now, Iâm using physics.
That has been requested before several times, Devil_Inside. And I agree with you. Especially on deferred rendering the amount of layers was insanely limited in the past - though I donât know if this has changed.
Thatâs a good point. I have never thought about that haha. So far my game only uses 6 layers so I donât have the need to think about something like that ahha.
I actually make heavy use of layers in my application, which is a real-time engineering visualisation tool. I use layers for camera culling and to perform the same functionality as layer tools in things like 3ds Max and AutoCAD - turn certain layers on and off, isolate etc.
Some of my projects have started to rub up against the layer limit and I was really hoping it would be increased for version 5, but unfortunately not.
BTW, if anyone asks why I donât just cache arrays of objects and turn the objects on and off, itâs because some of my objects can be quite complex and enabling them causes a significant pause, whereas camera culling is instantaneous. If thereâs any better way to handle this Iâm all ears, but the simple fact is that camera culling does this very easily and dare I say elegantly.
I ran into a similar situation a while back, what I ended up doing was just moving the objects out of the camera view. It was a UI visualization so the âlayersâ didnât interact or do anything other than display information. As yours, there were a lot/complex elements. Just adding/subtracting 1000 from the parentâs Y ended up being more performant than turning it on and off. Might be worth a try.