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

Problems with infinite BlockPlacements

Discussion in 'Scripting' started by TheDissapoint, Aug 6, 2022.

  1. TheDissapoint

    TheDissapoint

    Joined:
    May 11, 2022
    Posts:
    4
    Hi so I'm new to unity and I'm currently working on making an infinite flat plane made up of blocks using chunk to determine render distance, however this is three main problems with my code that i cant figure out why its happening.
    1) Countless more blocks overlap and spawn on each other than needed
    2) The Loading of the chunks/ blocks don't spawn around the player however spawn to the side of the player
    3) Due to the amount of extrablocks spawning framerate drops down to about 5 frames

    Image of the problem
    upload_2022-8-6_13-27-23.png


    Code (CSharp):
    1. public class Chunk : MonoBehaviour
    2. {
    3.     public GameObject ChunkManager;
    4.     public GameObject ObjectCord;
    5.     public int chunksize;
    6.     private int CurrentChunkX;
    7.     private int RawX;
    8.     private int CurrentChunkZ;
    9.     private int RawZ;
    10.     [SerializeField] int Maxchunks; // render distance
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         List<string> LoadedList = new List<string>();
    16.         RawZ = (int)(Mathf.Floor(ObjectCord.transform.position.z)); // rounds down
    17.         RawX = (int)(Mathf.Floor(ObjectCord.transform.position.x));
    18.        
    19.         //checks if RawX or RawZ is not 0, so when divided later errors dont popup
    20.         if (RawZ != 0)
    21.         {
    22.             CurrentChunkZ = (RawZ / chunksize);
    23.         }
    24.         else
    25.         {
    26.             CurrentChunkZ = 1;
    27.         }
    28.         if (RawX != 0)
    29.         {
    30.             CurrentChunkX = (RawX / chunksize);
    31.         }
    32.         else
    33.         {
    34.             CurrentChunkX = 1;
    35.         }
    36.      
    37.        
    38.         for (int p = (CurrentChunkX - (Maxchunks / 2)); p < Maxchunks; p++) // the X value on the 2d chunk loading table, made to be half so it loads around the player not in front
    39.         {
    40.             for (int o = (CurrentChunkZ - (Maxchunks / 2)); o < Maxchunks; o++) // the z value of the chunks
    41.             {
    42.                 string temp = ("X:" + p + ", Z:" + o);
    43.                 if (!LoadedList.Contains("X:" + p + ", Z:" + o))  //check if its not already added to the loaded list
    44.                 {
    45.                     LoadedList.Add(temp); // adds to loadedlist Chunks
    46.                     int LoadChunkX = p * chunksize;
    47.                     int LoadChunkY = o * chunksize;
    48.                     chunkload(LoadChunkX, LoadChunkY); // starts loading in that chunk
    49.                 }
    50.                 else
    51.                 {
    52.                        
    53.                 }
    54.  
    55.             }
    56.         }
    57.  
    58.     }
    59.  
    60.     void chunkload(int x, int y)
    61.     {
    62.         // places a block in each block position at each chunk
    63.         for (int PlacerX = 0; PlacerX < chunksize; PlacerX++)
    64.         {
    65.             for (int PlacerY = 0; PlacerY < chunksize; PlacerY++)
    66.             {
    67.                 //clones block and fills
    68.                 GameObject BoxClone = Instantiate(ChunkManager, new Vector3(x + PlacerX, ChunkManager.transform.position.y, y + PlacerY), ChunkManager.transform.rotation);
    69.                 BoxClone.name = "Box" + x + y;
    70.             }
    71.         }
    72.     }
    73. }
    74.  
    Heres is what the flow sheet looks like for both functions
    upload_2022-8-6_13-29-58.png



    Any input or help would be amazing thank you.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Any reason you don't use
    temp
    on line 43 when you check contains? That's just needless violation of DRY / SPOT , asking for an error.

    Beyond that, get ready to debug... if your "flow sheet" is accurate, then start figuring out how your program deviates from it.

    If your program doesn't deviate from the "flow sheet," perhaps your "flow sheet" is logically faulty.

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    If you're just making a minecraft clone, there are like ten billion tutorials out there... the entire process is pretty well understood now 20 years into the minecraft era. :)
     
  3. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    I don't know what expectations you have. But only a few people on the planet are able to spot logical/algorithmic errors in complex code just by looking at it. Even moreso when it contains several errors/bugs. And even less people are willing to debug the code for you for free. So Kurt's advise is correct. Your best option is to debug it yourself. Focus on one problem at a time. Verify your assumptions. See where it goes downhill.
    Or, alternatively, recognize that the problem/task is too complex for you and do something simpler first.

    And most of your thread tags don't really make sense (unityscript?).