- Home /
Best performance for mass object movement
I found myself doing some research before making a gun system in unity 2D ( Essentially a "rapid spawn object" system that I can make into whatever with a few extra behaviors), and I came across a couple threads which raised me some questions in terms of mass performance (I expect a maximum of 500 bullet objects on the screen at once, if the player works to "group enemies" with aggro in a way that I would not intend, ie. this should not be a bullet hell, but could be if the player is trying to break the game)
[]
What I'm wondering is "What is the best practice for scaling mass object movement?"
[]
I know of object pooling, and intend to use it. But I found this in regard to enabling objects here: https://forum.unity.com/threads/bullet-hell-without-performance-hit.568243/ where Brownboot67 said "Enable and disable are very slow. Just move the bullets very far away when you don't need them."
if this is true (and I see no reason why its not) then I would make a "DisabledObj" region off-map, which would have aMyDisabledObjManager script with a Dictionary of obj & bool, called disabledObjs. Then OnCollisionStay it would compare objects in region to the objs in dictionary, and if there was a new obj then it would add it to the dictionary and do obj.GetComponent().myIsActiveBool=false; In every gameobject in the region, in update, if (myIsActiveBool==false){return}; I could have the bullet remove itself from the dictionary when it gets reused if I make the MyDisabledObjManager scrip (which has the dictionary) a singleton, which every object that can be MyDisabled() has a reference to.
Is this a viable solution? Is there a "best practice" for object pooling without enabling and disabling gameobject? Is it even necessary?
[]
Another performance question arises due to collision. Rigidbodies are expensive, so as few objects as possible should have them. Does this mean that collision detection between a bullet and a wall/enemy/player is determined entirely by the object being hit, and none by the bullet itself? (aside from range in regard to timeout, but that's not collision). Is there a better way to handle collision detection than putting a Kinematic/ static Rigidbody on the root terrain itself? Is it actually a good practice to do that?
[]
Any insight, thoughts, and experience shared will be appreciated.
Answer by Arpi01 · Jul 25, 2019 at 11:33 PM
I think you should look into ECS, it was made for that kind of situations
Seems promising. I'll look into it, I haven't heard of ECS before now. Thanks!
I once done something similar, and changing from gameobject bullets to a particle system performance benefit was really really big. follow this simple tutorial and you will be able to do it without any performance issue
https://unity3d.com/es/learn/tutorials/topics/scripting/particle-collisions
Do what @xxmariofer proposed first, ecs is nice but difficult to learn. Good in-depth documentation is rare, any information older than half a year most likely outdated. there were a lot of API changes in the last year.
I looked a bit into ECS and I think I will try to learn it for future projects, but for right now, using the particle system should fit my needs.