Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Open world - loading and unloading objects within range of player?

Discussion in 'Scripting' started by adammil2000, Jan 13, 2015.

  1. adammil2000

    adammil2000

    Joined:
    Aug 25, 2013
    Posts:
    19
    Hi everyone!

    Are there any examples that load and unload game objects as they move in and out of a set distance from the player?

    This is for an open world game without zones and you need to load and unload game objects in the scene as the player gets near them, and also unload them once they are out of the range of the player to see or interact with.

    Thanks!
     
    blamb888 likes this.
  2. Haseeb_BSAA

    Haseeb_BSAA

    Joined:
    Aug 20, 2014
    Posts:
    316
    Actually this system is already implemented when you make a game like that in Unity. I'm also developing an open world survival horror game and when I move far from the mountains , at a specific distance , they disappear and when I go closer , they appear again. I don't think you need any kind of code for this.
     
  3. adammil2000

    adammil2000

    Joined:
    Aug 25, 2013
    Posts:
    19
    I mean loading and unloading objects from memory, not just a visual effect that hides those distant items.
     
    Crossway, blamb888 and Haseeb_BSAA like this.
  4. Haseeb_BSAA

    Haseeb_BSAA

    Joined:
    Aug 20, 2014
    Posts:
    316
    Well you can probably do that by writing a script , for example set the bool values in such a way that if the distance is less or greater than a certain point , make the objects appear or so. Why do you need that btw? :)
     
  5. adammil2000

    adammil2000

    Joined:
    Aug 25, 2013
    Posts:
    19
    A giant open world map that cannot load everything into memory at once because it is too big, so have to load and unload objects dynamically as they get near the player.
     
    blamb888 and Haseeb_BSAA like this.
  6. Haseeb_BSAA

    Haseeb_BSAA

    Joined:
    Aug 20, 2014
    Posts:
    316
    Oh my :O Please inform me when you have updates of your project , I'm interested in Open World games and hopefully that's what my next project is gonna be. :)
     
    blamb888 likes this.
  7. inko

    inko

    Joined:
    Nov 29, 2013
    Posts:
    143
    There are a couple of ways to achieve this. You can either write your own level file loader and store an array of object ID + position in a file. Then at runtime you would find the correct chunk files based on their name, serialize them back into struct and instantiate the objects at real time based on their ids + position + rotation.

    Code (CSharp):
    1. struct SerializableObject {
    2. int id;
    3. Vector3 position;
    4. Vector3 rotation;
    5. }

    Sounds complicated but it's actually pretty straight forward.

    An other approach would be having every zone stored as a prefab and instantiate them based on a 2d chunk array. Though the lack of proper prefab nesting makes this method bit of a mess.

    You'd get your current chunk with something like this:

    Code (CSharp):
    1.  
    2. chunkSize = 64;    // = 1 chunk is 128 meters across
    3. Vector2 chunkPos = new Vector2( Mathf.FloorToInt(hero.position.x / chunkSize) , Mathf.FloorToInt(hero.position.z / chunkSize) );
    4.  
    5. if(chunkPos != lastChunkPos)
    6. UpdateChunks( chunkPos );
    7.  
    8. void UpdateChunkPos (Vector2 v) {
    9. // however you wanna store your chunk, load if from here
    10. }
    11.  
    No matter what approach you take you will have to rely on zoning in same way or another.

    Hope this helps.
     
    adammil2000 likes this.
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,184
    Use Application.LoadLevelAdditive! Unlike LoadLevel, it doesn't destroy your old level objects.

    So, in every scene that's a part of the world, you create one main object that you put as the parent of every other object in the scene. Then, when you're getting close enough to the border of your current level, you use LoadLevelAdditive on the next scene, find the parent object of that scene, and move it to where it's supposed to be in relation to your current level.

    Then, when you need to unload a level that you're far away from, just call Destroy on the parent object of that level.

    Note that this will cause the game to hang as you're waiting for the next level to load. If that's a problem, there's LoadLevelAdditiveAsync, which will just run along in the background while you're playing.
     
    tbowers22, blamb888 and adammil2000 like this.
  9. depperm

    depperm

    Joined:
    Apr 3, 2014
    Posts:
    1
    Won't this be very load/memory intensive if you have a large world? Whenever the character moves iterate over all the objects and load those within range and unload those outside of the range?
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    @depperm - careful about necroposting. This is a 2 year old thread, and every single comment posted here is ether obsolete (e.g.Application.LoadLevelAdditive is deprecated in favor of the new scene management classes, which are much better for this sort of situation anyway because they allow you to unload scenes as well), or was never good advice to begin with.

    To more directly answer your question, you would probably want to limit the objects you're searching to a certain area, which would best be done by loading and unloading scenes around you. If you create a 'grid' of scene files named appropriately (e.g. Landscape03x04.unity), you can use your position to know that you're in the 03x04 landscape scene, and you should load all the surrounding scenes. This would take no additional memory or CPU time no matter how many of these scenes are in your world, since you're just picking the desired filenames.
     
  11. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
    you can make a big trigger around the player and do like if a building is outside the trigger its destroy the building



    10/10 english
     
  12. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    I am unable to understand that what to do with chunk position variable? I tried to implement it and getting chunk position but actually it is?
    Code (CSharp):
    1.     public Vector2 chunkPos;
    2.  
    3.     void GetPlayerPosAccordingToTile()
    4.     {
    5.         Transform cameraPlayer = this.transform;
    6.         int chunkSize = (int) tileSize.x;
    7.         chunkPos = new Vector2(Mathf.FloorToInt(cameraPlayer.position.x / chunkSize), Mathf.FloorToInt(cameraPlayer.position.z / chunkSize));
    8.     }
     
  13. unity_483C4357D3269025FA3B

    unity_483C4357D3269025FA3B

    Joined:
    Jul 2, 2022
    Posts:
    20
    @StarManta necroing posts is necessary when the post is first hit on google. People might think this is still relevant when it's not. It's not 1999 anymore where people browse forums. People use google, whatever is most relevant comes up, regardless of post date.