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. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Question Help building structures with cubes using code.

Discussion in 'World Building' started by dnlblasco06, Dec 6, 2022.

  1. dnlblasco06

    dnlblasco06

    Joined:
    Jul 3, 2021
    Posts:
    7
    Hi. I am trying to do a small experiment in which I would like to build structures with simple cubes using code.

    I found this code on GitHub, which makes some simple trees:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public static class Structure
    6. {
    7.     public static void MakeTree(Vector3 position, Queue<VoxelMod> queue, int minTrunkHeight, int maxTrunkHeight)
    8.     {
    9.         int height = (int)(maxTrunkHeight * Noise.Get2DPerlin(new Vector2(position.x, position.z), 250f, 3f));
    10.  
    11.         if (height < minTrunkHeight)
    12.         {
    13.             height = minTrunkHeight;
    14.         }
    15.  
    16.         for (int i = 1; i < height; i++)
    17.         {
    18.             queue.Enqueue(new VoxelMod(new Vector3(position.x, position.y + i, position.z), 6));
    19.         }
    20.  
    21.         //leaves
    22.  
    23.  
    24.         for (int x = -2; x < 3; x++)
    25.         {
    26.             for (int z = -2; z < 3; z++)
    27.             {
    28.                 queue.Enqueue(new VoxelMod(new Vector3(position.x + x, position.y + height - 2, position.z + z), 11));
    29.                 queue.Enqueue(new VoxelMod(new Vector3(position.x + x, position.y + height - 3, position.z + z), 11));
    30.             }
    31.         }
    32.  
    33.         for (int x = -1; x < 2; x++)
    34.         {
    35.             for (int z = -1; z < 2; z++)
    36.             {
    37.                 queue.Enqueue(new VoxelMod(new Vector3(position.x + x, position.y + height - 1, position.z + z), 11));
    38.             }
    39.         }
    40.         for (int x = -1; x < 2; x++)
    41.         {
    42.             if (x == 0)
    43.                 for (int z = -1; z < 2; z++)
    44.                 {
    45.                     queue.Enqueue(new VoxelMod(new Vector3(position.x + x, position.y + height, position.z + z), 11));
    46.                 }
    47.             else
    48.                 queue.Enqueue(new VoxelMod(new Vector3(position.x + x, position.y + height, position.z), 11));
    49.         }
    50.  
    51.     }
    52. }
    53.  
    I have tested the whole project (which is on GitHub) and it works, but I can't understand how it works. If someone can explain it to me I would appreciate it a lot to be able to make my own structures.
     
  2. dnlblasco06

    dnlblasco06

    Joined:
    Jul 3, 2021
    Posts:
    7
  3. dnlblasco06

    dnlblasco06

    Joined:
    Jul 3, 2021
    Posts:
    7