Search Unity

Question Problems with chunk based infinite world loading

Discussion in 'World Building' started by LegoDude, Nov 7, 2020.

  1. LegoDude

    LegoDude

    Joined:
    Nov 14, 2015
    Posts:
    12
    Hello all. Recently I've been working on a sort of chunk-based infinite map generation for my game. The essence is that when the player moves in any direction, new chunks (predefined prefabs that I have assigned to game objects) will load in random order. There is no need to require them to perfectly mesh as the prefabs naturally do this so they only need to spawn randomly. As the player moves the player should always be at the center of a 5x5 grid of these prefabs. This means as the player moves around, new ones will load and old ones will get unloaded. The positions of the prefabs need to be saved so that if the player goes back to a certain position, the same prefab that was there initially needs to be there. We are having a few problems with this however and the generation code has been attached to this thread.

    Problems 1:
    When the player initially spawns in the map generated is a 6x6 instead of a 5x5 and the player therefore cannot be spawned in the middle. See the image below for reference where the yellow circle is the player. Each color is one of 3 prefabs. Why is this happening and how can it be resolved.
    upload_2020-11-7_14-28-48.png
    for reference here is the inspector view for the script.
    upload_2020-11-7_14-31-53.png

    Problem 2:
    When the player moves around, the prefab spawning and despawning isn't perfect. Sometimes the loaded chunks are 5x4 or 6x5 or 5x6 etc. It isn't the even 5x5 it should always be. Furthermore, the generation is slow, it takes a long time before it decides to spawn in new chunks and this can cause issues as the player will be moving very fast (vehicle).
    Why is this happening and how can it be resolved. This video should showcase how the generation is failing: https://cdn.discordapp.com/attachments/457344681588686879/773810365368631306/2020-11-05_02-23-47.mp4

    Attached is the generation script

    I understand this is a large task and does not have one single simple answer. As much help in understanding or resolving our issues as possible would be greatly appreciated. Thank you for your time.
     

    Attached Files: