Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Enabling/Disabling objects in a radius around the player to improve performance

Discussion in 'Scripting' started by flipwon, Apr 23, 2019.

  1. flipwon

    flipwon

    Joined:
    Dec 29, 2016
    Posts:
    93
    Is this a plausible thing? I'm making a top down 3d game, and with a lot going on when the levels get big, the game starts to lag.

    What is the best way to go about this?
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    2,437
    You'll probably want to use the profiler to determine what things are actually causing the slow down. The approach could work (with limitations) in some of the cases, but in other cases it would either be infeasible, or redundant given simpler, built-in ways to improve performance. For example:
    • Is the issue with rendering? Have you considered occlusion culling instead of actually enabling/disabling objects?
    • Is the issue with physics contacts? In that case, turning things off could cause really weird results with the physics simulation, and probably isn't a great idea.
    • Is the issue with behavior on scripts you wrote? Consider whether you should be fixing the underlying performance problems in your scripts.
    • Is the issue having hundreds or thousands of objects, all executing Update() every frame? This happens with bigger scenes, and the usual advice here is to get rid of the Update() method, and create a separate manager class to update the entities.
    Anyway, turning objects on and off like that can also result in framerate hiccups, so it might not be an overall improvement. I'd recommend taking a closer look at the specific cause of the performance issues first.
     
  3. N_Murray

    N_Murray

    Joined:
    Apr 1, 2015
    Posts:
    98
    Joe-Censored and APSchmidt like this.
  4. flipwon

    flipwon

    Joined:
    Dec 29, 2016
    Posts:
    93
    Hey there, thanks for this. Honestly I'm new to unity, I've only tried it briefly in the past so things are still a bit of a splash in the face to me.

    Using the Profiler it seems to be the rendering that is hogging up everything, I think? It's taking up 5.5ms to the nearest being scripts at 0.36.

    I have quite a few animated 3d voxel "tiles" so I'm assuming these are being animated off camera. Disabling them in the editor really speeds things up so I figured enabling/disabling would work. Would looking into occlusion culling be worth it? It seems very complex.
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    2,437
    Occlusion is pretty easy. Much easier than I expected. Just a couple of things to set up:

    https://docs.unity3d.com/Manual/OcclusionCulling.html

    That said, it might not make a big difference depending on how you have your game structured. Unity already does frustrum culling automatically, which means that if objects are left/right/above/below the field of view of the camera, they already won't be rendered. Occlusion culling is mainly for when there are a lot of objects in front of the camera, but they're hidden (occluded) by nearer objects. Without occlusion culling, Unity will render all those hidden objects. But in your case, if you're doing top-down, it's likely there aren't many things happening behind other things?

    By default, I don't think Unity animates things off screen. For skinned mesh renderers, there's a toggle if you want that behavior, but it's off by default. On Animators, there's a Culling option to control whether animations keep rendering while off screen. If that's set to "Always Animate" for your objects, you should consider changing it.
     
    flipwon likes this.
unityunity