Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Feature Request Access to LODGroup distance to camera

Discussion in 'Editor & General Support' started by Ashanex, Mar 10, 2024.

  1. Ashanex

    Ashanex

    Joined:
    May 2, 2019
    Posts:
    29
    Hello!

    Is there a float distance to camera value hidden inside the LODGroup component? And if there is, could you expose it so that we can just get that instead of having to recalculate manually the distance?

    I'm making a game with 10000+ units on screen, each with their own LODGroup, if the distance to camera is already being calculated there, I would appreciate to be able to access it and not waste performance on recalculating it again. (It takes 0.6ms+ to do it from scratch with a Vector3.Distance).

    Note: I could calculate it with jobs over a few frames but it would make some areas of the game less responsive and that's undesirable.

    Thanks for your time!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    7,263
    10000+ units => sounds like a case for Entities. If it's one prefab with several GameObjects and components per unit plus animations, you are unlikely to get to 10k instances before fps drops below 60. If you haven't done a performance test with all per-unit features, do that. The relatively humble 0.6ms for distance checks may be the least of concern. ;)
    Definitely use Burst + Mathematics + Collection + Jobs for all performance critical math calculations and iterations.

    You want to use spatial hashing so you can group these units into roughly the same distance if you don't need the precise distance. Pretty sure I've come across jobs-enabled, bursted spatial hash collections on GitHub.

    You can also skip distance updates for some frames by utilizing domain knowledge of your game. For instance, if the camera is a fixed top-down camera and didn't move in the past frames, and units only move n units per frame, you could use the same distance for units over several frames until they passed some time or distance threshold.
     
    Ashanex likes this.
  3. Ashanex

    Ashanex

    Joined:
    May 2, 2019
    Posts:
    29
    Hey thanks a lot for answering! I didn't consider using the Mathematics package, is it a free performance boost just by using it instead of MathF?

    I do use jobs and burst, although frankly it's uses have to me always been pretty limited because there's always a roadblock to specific issues that I'm trying to solve with them. But maybe it's my fault because I'm not using them correctly.

    I've worked with entities in a separate project at work for a year and a half and I cannot see myself doing a full game with it, too complex, lack of documentation and community answers online, and nothing close to intuitive.

    I'll look into the spatial hashing that sounds interesting!

    I still believe that Unity should make things like the camera distance available anyways in order to be able to maximize performance without having to do a full brain transplant of the game and your code methodology to ECS.

    Also it's kind of annoying that Unity pushes us to ECS instead of optimising the base engine in the first place. Like for instance the NavMeshAgents pathfinding being calculated single-threadedly.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,661
    Just remember that the LODGroup, and anything to do with distance to camera, will depend heavily on how many cameras are doing the rendering. LODGroup recalculates distance for every camera that can possibly render the object, every frame, whether it's the main camera or not. Most gameplay uses will really only be interested in the distance to the main camera.
     
    Ashanex likes this.