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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Little doubt with terrain generator

Discussion in 'Scripting' started by PedroMBP, Dec 19, 2018.

  1. PedroMBP

    PedroMBP

    Joined:
    Feb 25, 2017
    Posts:
    21
    Hello everyone, everything good ?
    I am developing a small terrain generator, I am using blocks for now, but for the future I intend to increase something else.
    But my question at the moment is that I have a little bug.
    Some blocks are generated within the other, would anyone have any idea how could fix this problem? it works partially, but with blocks overwritten in their positions.
    Thank you!

    Code and image the terrain.




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class TerrainBehaviour : MonoBehaviour {
    7.  
    8.  
    9.     public GameObject hexTransform;
    10.  
    11.     public static int maxBlocks = 1000;
    12.  
    13.     //Ignore
    14.     //public float xOffset = 1;
    15.     //public float zOffset = 1;
    16.  
    17.     //public float xOffset = 0.866f;
    18.     //public float zOffset = 0.75f;
    19.  
    20.     public NavMeshSurface surface;
    21.  
    22.     int xX = 0;
    23.     int zZ = 0;
    24.  
    25.  
    26.    
    27.  
    28.     public void Start()
    29.     {
    30.  
    31.  
    32.         this.gameObject.isStatic = true;
    33.  
    34.  
    35.         for (int x = 1; x < maxBlocks; x++)
    36.         {
    37.  
    38.             Vector3 pos = new Vector3(xX, 0, zZ);
    39.  
    40.            
    41.  
    42.             while (pos.x == xX && pos.z == zZ )
    43.             {
    44.                 pos.x = Random.Range(xX - 1, xX + 2);
    45.                 pos.z = Random.Range(zZ - 1, zZ + 2);
    46.             }
    47.  
    48.             instantiateGrass(pos);
    49.      
    50.            
    51.  
    52.  
    53.         }
    54.  
    55.         surface.BuildNavMesh();
    56.  
    57.     }
    58.  
    59.  
    60.     void instantiateGrass(Vector3 pos)
    61.     {
    62.         xX = (int)pos.x;
    63.         zZ = (int)pos.z;
    64.        
    65.         GameObject go = Instantiate(hexTransform, new Vector3(pos.x, pos.y, pos.z), Quaternion.Euler(-90, 0, 0));
    66.  
    67.         go.name = "Hex: " + pos.x + " / " + pos.z;
    68.         go.transform.SetParent(transform);
    69.  
    70.         go.isStatic = true;
    71.     }
    72.  
    73.     public void Update()
    74.     {
    75.  
    76.         print(Random.Range(-1, 2));
    77.     }
    78. }
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
  3. PedroMBP

    PedroMBP

    Joined:
    Feb 25, 2017
    Posts:
    21
    Thank you!
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    1) make yourself a list of position. ( List<Vector3> )

    2) everytime you wanna spawn a new block, get the position for it, check if it's already in the list get new position

    3) spawn block, add position to list

    4) $$$ (profit)

    Totally, all I could think during reading the post is "How the F*** does he remember this username", before I clicked it I suspected it was just some advertisement bot.

    In all seriousness, how did you get that username?
     
  5. PedroMBP

    PedroMBP

    Joined:
    Feb 25, 2017
    Posts:
    21
    Great idea, how did not I think of this before?
    Thank you!
     
  6. PedroMBP

    PedroMBP

    Joined:
    Feb 25, 2017
    Posts:
    21
    Hi! Are you okay guys? I took that same topic to include another question.
    How do I make biomes, the main goal would be to put different textures on the map?
    Anyone have any suggestions?
    thankful!
     
  7. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Its better to start a new tread with an appropriate title, also, again:

     
  8. PedroMBP

    PedroMBP

    Joined:
    Feb 25, 2017
    Posts:
    21
    Obrigado pelo conselho, vou criar de novo!
     
  9. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    מה אחי