- Home /
Destroy and instantiate far objects
Hello, I have simply question, I want to destroy far map objects (or just make them inactive) and instantiate (or set active) them again when I'm getting closer. What will be the best for performance. Checking for all objects vector3.distance or make huge sphere around of player and then OnTriggerEnter and Exit? Or somethink else?
Answer by TreyH · Apr 30, 2018 at 04:42 PM
The trigger route would require that every ignored / distant object has a trigger collider, which might not be fun to set up.
There are camera settings for this sort of thing, so I don't know how much effort you actually need to do manually. Unity does quite a bit for you out of the box.
For your question, Octrees are useful for these sorts of things, and Unity's own Terrain system uses them iirc. With an Octree (or Quadtree in 2D), you break the world into a series of blocks. Each block is then checked for whether or not it contains a number of objects (a number you set, we'll call this N). If that block contains N or more objects, it's subdivided into 8 (or 4 for 2D) smaller blocks, which then undergo the same check. Performance-wise, these trees can be produced in near-linear time.
How often you produce / update the tree is up to you. Once a tree is produced, you wouldn't check for distance to individual objects -- you would check distance between the block with your camera / player versus other blocks, disabling everything within blocks that are too far.
Sounds logical but one question came to my $$anonymous$$d, is it necessary to destroy objects if they are static, without rigidbody and scripts and they are out of camera? Will somehow unity to process them? Octrees at this time are to complicated to make for me.
The trigger route would require that every ignored / distant object has a trigger collider, which might not be fun to set up.
Is it not enough to just add trigger collider to objects? Even if I just started making map and placing objects?
If you're O$$anonymous$$ with everything having a trigger collider, then your sphere solution would work just fine. I didn't know how large your scene was going to be (100 objects vs. 100,000 etc.).
I doubt that you'll need / want to destroy an object outright. Disabling an object will suspend it from physics, stop its Update calls, prevent rendering, etc.
One performance consideration is that you should probably not use Vector3.Distance
or a Vector3 instance's .magnitude
for your distances. Both of those involve square root calculations which are slow, but you can use .sqr$$anonymous$$agnitude
to avoid this.
$$anonymous$$y scene will be big I think. But I will have to add to every object box colliders manually so add one more tagged as trigger will no be an extra large job. I just hope that ontriggerexit will not miss any object by mistake. About distance, you are right. I will change every vector3.distance to sqrmagnitude in my scripts. Thank you
Answer by SharkoFR · Apr 30, 2018 at 04:57 PM
The best way is simply to use LOD: https://docs.unity3d.com/Manual/LevelOfDetail.html
The camera is from top to bottom like in gta1, I think lod are not necessarily