Lots of objects "listening" for collisions...how can I handle it better?

Hello everybody.

I am still messing around with unity and trying to understand all of it.
I am currently testing collisions n stuff.

Basicly I took some Zelda Sprites (GameBoy) and am now re-creating the engine.
For now, everything is fine…however, I have some questions bout performance with lots of hitboxes (BoxCollider2D)

As you can see in the screenshot I got lots of bushes.

I have a sword that can slash the bushes and simply make em “Destroy” …no magic here.
However, when I run this sandbox it shows bout 77 FPS…which is really low for such a small sandbox-scene I think…

So, how would I handle lots of objects like this that are just “standing” there waiting for a collision with a specific entity? In the inspector you can see the objBush properties.

Thanks in advance :slight_smile:

A couple of things that are important for Unity development. First and foremost is the minimization of game objects when necessary. Secondly, minimizing the use of Instantiate and Destroy and using pooling to assist with this. Thirdly, using the SetActive property of game objects and the enabled property of components to further minimize load.

For example if you are making a Zelda style clone then surely you are using some sort of grid based tile system? You can have a map made up of millions of tiles if you wanted to, and only need to display a fraction of them (i.e. 128). With that said it is critically important to remember that for every sprite / tile you have you will need a game object. But if you have game objects off-screen and not visible why not just re-use those? Also, to handle depth of your map (i.e. bush on top of grass = 2 game objects) you can build your map into layers with different levels of visibility and use the same recycling / pooling system you used for your tiles to accomplish it.

As a personal opinion, I wouldn’t be concerned with the FPS unless it drops below 60. If that should happen, Unity’s tools like the profiler will assist you in finding the reasoning behind it. To get a feel for your base FPS start a new project and leave it completely empty and just hit the play button and view the stats. You will find the FPS fluctuates wildly (as it always does) but it never really goes all that high because of VSYNC and other technologies. It will almost always hover between 60 - 90 fps even with a beast of a computer. Also keep in mind that stats fluctuate wildly during a play test and do not perform the same as your compiled game will due to debugging and other things.

Thanks for the reply.
I learned about object pooling in one of the lesson videos… do you have any examples for the other techniques and how to approach this?

Thanks in advance!

if you turn off vsync in the editor you will see that you got higher fps^^

edit → project settins → quality → VSync Count

Thanks…I tried it.

Without Vsync and with all bush objects removed I get like 3600 FPS
However, will all of those bush instances I have like 2600FPS …so, 1k FPS difference just cause those bushes? :frowning:

Keep in mind that vsync synchronizes your frames to your monitor’s refresh rate and limits the excess. Disabling vsync can cause it’s own set of problems and as it currently stands it doesn’t even sound like you have one FPS wise. I would follow the old adage if it’s not broken don’t fix it. But that’s just me.

Going back to pooling…there are many different approaches to create the type of game it sounds like you are trying to make. Pooling itself is incredibly easy to understand, but I don’t feel it is what you need to learn to solve your problem at this very moment. As a matter of fact without proper tiling it will do you little good at this moment. What you really need to learn is proper tiling. As a matter of fact I couldn’t even show you proper pooling without knowing the way your system works. I could only show pooling as a method of my own approach and that will take a huge deal of effort. As I said, you need to figure out the tiling first. It is usually much more difficult to understand and learn.

The system you need is a different type of pooling system. It will fill the camera view area with gameobjects (sprites/tiles) for every layer of depth your map has. When the tile won’t be displaying any sprite then it is disabled otherwise it is enabled. As the player moves you will update the tiles with their appropriate tile data for that location.

Well, fortunately I am just messing around to check unitys 2D capabilities …no game planned yet :smile:
I will try to find some tutorial for explaining the tiling system u mentioned. Thank you

With a Zelda-type game you wouldn’t normally use actual colliders, but just use an array to determine if tiles are passable or not. This demo uses some thousands of “colliders”, but since they’re just bits in memory that don’t do anything actively, you can have millions (or any number at all) with the same speed.

–Eric

I will make sure to check the demo.
Thanks a lot :slight_smile: