Search Unity

Chunk loading?

Discussion in 'Scripting' started by VirusXProgramming, Jan 26, 2015.

  1. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    I have a game where the player is able to move infinitely.
    How would I make it generate a prefab planet and asteroid but when you get far away from the "terrain" they unload so the game works well. - Sort of like the game minecraft.
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
  3. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    I'm not sure if occlusion culling would help, ofcourse it's a good tool but it expects the scene to be fully constructed and baked before runtime if I'm not mistaken?

    I would create a class that check the distance between the camera and the chunk once a second or so, if the distance is far enough I would disable the chunk and Place it in an object pool.

    There's probably a much better way to solve it though, but not that I know of myself. >,<
     
  4. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    Thanks for the reference but that's not exactly what i'm looking for. I need to actually disable the object because I have some scripts that start to dramatically lower the FPS. That would just be the renderer - right? If i am mistaken sorry
     
  5. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    Just one question. I have no idea where to start writing this script (sorry) but what is an object pool?
     
  6. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    Good idea by the way!
     
  7. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    you could do something like this:
    it needs your player to have the tag "Player" and your planets the tag "Planet"
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DeactivateByDistance : MonoBehaviour
    5. {
    6.     public float distanceThreshold=10;      //low number for testing purpose, you could set this to your camera's far plane.
    7.     private GameObject[] planets;
    8.     private GameObject player;
    9.  
    10.     void Start()
    11.     {
    12.         player = GameObject.FindGameObjectWithTag("Player");
    13.         planets = GameObject.FindGameObjectsWithTag("Planet");
    14.     }
    15.     void Update()
    16.     {
    17.         foreach (GameObject planet in planets)
    18.         {
    19.             if((player.transform.position.magnitude-planet.transform.position.magnitude) > distanceThreshold)
    20.                 planet.SetActive(false);
    21.             else
    22.                 planet.SetActive(true);
    23.         }
    24.     }
    25.  
    26. }
    27.  
     
    VirusXProgramming likes this.
  8. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    :p At the moment I''ve too Little time to explain in depth, but here's a link to a video explaining some basic object pooling:
    http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling
    Object pooling is quite common in game design so if you google for it you should surely find some matches on even more documentation and tips aswell. :)
     
    VirusXProgramming likes this.
  9. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    Thanks jister but would I put this on an object like the main camera or should I put it on something else?
    Also to put multiple things to get spawned with different rarities - How would that work?
    EDIT
    This doesn't spawn the planets randomly - how does that work?
     
    Last edited: Jan 28, 2015
  10. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
  11. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    VirusXProgramming likes this.
  12. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    Thanks a lot!
    Any Idea how I would make this 2D Voxel terrain though. I searched it up and couldn't find anything. :(
     
    Last edited: Jan 28, 2015
  13. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    And yes I am going to bump this. - I haven't yet and it is kind of urgent
     
  14. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
  15. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    do a search on the tutorial page for 2D... it's the same principle only use quads instead of cubes...
     
  16. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    Could you do show me something to follow that's meant to be 2d please? I'm very bad with code so sorry though And I also want to have some space inbetween planets. Like solar systems. Any chance i could use a prefab insteas of a cube?
     
  17. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    is your game 2D or 2.5D? are you going to use sprites and stuff or just an orthographic camera?
    if it's the second you just follow along with the tutorial, I recommend just doing the tut as is and once finished do it again and adapt to your needs.
    but i've you have that little experience maybe you should first learn to code better before starting on such advanced stuff.
    why not make your game a bit smaller and not generate a universe procedurally?
     
  18. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    My game is 2d and has sprites. I know how to code in java so i figured I would try c# or JS. JS turned out to be pretty weird so I am adapting to c#.
     
  19. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It sounds like you're in a bit over your head. Start smaller to - if nothing else - get a grasp of Unity's workflow and structure. Check out the Learn section, then come back and ask lots of questions :)
     
  20. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    i just want a start please
     
  21. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The Lean section is your best starting point.
     
  22. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    Lean?
     
  23. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
  24. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
  25. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Learn. Also don't bump posts. Also here's the first video in a great series on how to make your first game project.
     
  26. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It's linked at the top of this page, right next to the Community link.

    Quite frankly - it's troubling that me mistyping the word Learn (despite typing it correctly in a previous post) held you up for three days. It gives the impression that you're not willing to take the initiative when it comes to learning and understanding the concepts necessary for this type of work. Everyone here is willing to help you, but you have to put in the legwork yourself first. If you don't want to do that (and that's perfectly ok!) then head over to the Collaboration section and try to find someone to work with.
     
    Blitz54 and jister like this.
  27. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    Well thanks for showing me the Learning section. I'll start from there :)