- Home /
How do I optimize my explosion script?
I am making a 2D game where you build ships with blocks and fly them around and shoot other ships. I am making explosive projectiles for the ships to shoot, but if I shoot a missile at ship and the AOE hits >50 blocks, it creates a large lag spike. Even smaller explosion radii can lag badly.
I have tried using Physics2D.OverlapCircleAll and FindObjectsOfType (and then looping through them to get the blocks in range).
Thanks for the help!
Answer by tuinal · Jul 20, 2020 at 04:16 AM
It would probably help if you post the code, since there could be many different causes.
FindObjectsOfType is not the way to go. Iterating Collders[] should work; you should optimise this by putting the blocks in a layer and testing the OverlapCircleAll only against this layer, so you're not then having to check if everything in the array is a block.
Instantiate is expensive so if you're spawning explosions that could also be a cause; it can be better to keep them in a pool of reusable objects rather than instantiate them.
You could also use a coroutine and crunch though the array over several frames to spread the load with negligible visual impact.
Your answer
![](https://koobas.hobune.stream/wayback/20220613003551im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Physic function doesn't work in different resoultions 0 Answers
2D Physics Thrower Side Scroller - Explosion Physics? 1 Answer
physics Material 2D not working !!!! 1 Answer
[Solved] How to touch/activate 2D objects? 1 Answer
How do you detect if two specific objects are touching each other without collision. 3 Answers