Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to disable game Objects based on distance with ECS

Discussion in 'Scripting' started by Marcos-Elias, Sep 29, 2019.

  1. Marcos-Elias

    Marcos-Elias

    Joined:
    Nov 1, 2014
    Posts:
    159
    Hello, I'm using Unity for years but I'm completely newbie at DOTS.

    How difficult is making a system in ECS to disable far game objects, based on distance from the camera?

    Something like this, but using multiple cores:

    foreach (GameObject g in list) {
    float dist =... (distance between the obj and camera)
    g.SetActive(dist < threshold);
    }

    Basically by putting some objects into a list and cycling through all of them, enabling or disabling them if they are at certain distance.

    I have a scene loaded with user mods (the world), and to save performance I disable objects far away, since all of them are loaded at once (by design, I got no hickups after they are loaded).

    Currently I use individual scripts with Invoke Repeating in each object with a random time so they won't check all together, this works well for hundreds or even thousands game objects, but not hundreds of thousands or millions.

    A foreach loop to enable or disable all together into a single frame would be worse. So some objects checks its distance between 5 and 10 seconds (smaller objects far away), other less time (rocks, lamps, large buildings etc).

    Player will never see they appearing due to the camera far clip plane (for large objects the threshold value is larger than far clip).

    It is running smoothly, but I would like to increase a lot the number of objects, so ECS comes in hand.

    I need they all disabled at far distance since they may contain sounds or scripts, not just mesh renderers. With mesh renderers only it seems easier.

    I cannot use LOD or other systems, just enable and disable them, since they are loaded at runtime as user mods where I have no control in editor.

    Do you have an idea on where to start or how to achieve this?

    Thanks in advance!
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    ECS is a separate system from the normal Unity workflow, so no GameObjects. And since most functionalities of GameObjects are part of the Unity API, you couldnt do a whole lot with them on any other threads than the mainthread anyways. Unless it's something like manipulating their positions, for which i believe there is an option in hybrid ECS. But to be honest, i mostly worked with Jobs and Burst so far, and little with ECS. So maybe i'm missing something and somebody corrects me. I believe you can convert GameObjects to Entities rather easily, but if there are scripts attached to them you need to convert these as well and so on.
    If you want to work with GameObjects, then to my knowledge, you need to do so on the main thread, so you could only use concurrency (with Coroutines), but no true parallelism.

    If you switch to full DOTS then handling tens of thousands of objects is easy for any decent computer, without the need to even disable them. Loading also does not impact the frame rate. It all just happens pretty much parallelized and super fast.
     
    Last edited: Sep 29, 2019