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 have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Creating grid based terrain.

Discussion in 'Scripting' started by Wombat85, Jun 24, 2015.

  1. Wombat85

    Wombat85

    Joined:
    Mar 27, 2015
    Posts:
    10
    Hey all, I have the following script that will create grid based terrain around the player. The problem is that it is creating it over and over. I want to add a check to see if there is already a block at that grid and if so, dont create a new block. I would also like to make it so old blocks are removed as the player moves away. Thanks for any help.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Random = UnityEngine.Random;
    5.  
    6. public class tileGenerator : MonoBehaviour {
    7.    
    8.     public int xTerrain;
    9.     public int zTerrain;
    10.     public float scale;
    11.     public float resolution;
    12.  
    13.    
    14.     private Transform boardHolder;
    15.    
    16.     public GameObject[] ground;
    17.     public GameObject[] water;
    18.    
    19.    
    20.    
    21.     // Use this for initialization
    22.     void Update () {
    23.        
    24.         GameObject player = GameObject.Find ("Player");
    25.        
    26.         //Find the players x and z location.
    27.         int playerX = (int)player.transform.position.x;
    28.         int playerZ = (int)player.transform.position.z;
    29.        
    30.         //Holds the gnerated tiles in a heading called board.
    31.         boardHolder = new GameObject ("Board").transform;
    32.        
    33.  
    34.             //For each square in range instantiate a prefab at that point, using the perlin noise as a highmap.
    35.             for (int x = playerX -xTerrain; x< playerX + xTerrain; x++) {
    36.                 for (int z = playerZ-zTerrain; z< playerZ + zTerrain; z++) {
    37.                    
    38.                         float xCoord = x / resolution;
    39.                         float zCoord = z / resolution;
    40.                
    41.                         //Generate a hightmap
    42.                         float height = Mathf.PerlinNoise (xCoord, zCoord);
    43.                
    44.                             if (height < 0.6) {
    45.  
    46.                        
    47.                                 //Randomly pick a tile from the array to generate.
    48.                                 GameObject toInstantiate = water [Random.Range (0, water.Length)];
    49.                        
    50.                                 //Generate it
    51.                                 GameObject instance = Instantiate (toInstantiate, new Vector3 (x, 0, z), Quaternion.identity) as GameObject;
    52.                                 instance.transform.SetParent (boardHolder);
    53.                            
    54.                            
    55.                            
    56.                             }
    57.                
    58.                             if (height > 0.6) {
    59.  
    60.                                 // Randomly pick a tile from the array to generate.
    61.                                 GameObject toInstantiate = ground [Random.Range (0, ground.Length)];
    62.                            
    63.                                 //Generate it
    64.                                 GameObject instance = Instantiate (toInstantiate, new Vector3 (x, 0, z), Quaternion.identity) as GameObject;
    65.                                 instance.transform.SetParent (boardHolder);
    66.                                
    67.                                                        
    68.                             }
    69.  
    70.  
    71.                
    72.                            
    73.             }
    74.         }
    75.     }
    76. }
    And what I am creating so far:

    upload_2015-6-24_12-19-8.png

    Like I said though, I need it to only instantiate if a block isnt already there. Thanks!
     
  2. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    Use a 2-dimensional array to keep track of your terrain tiles. You could make it be of Type GameObject[,] and assign the previously instantiated tile to the xy position, and check if a xy position is null or not if you want to find out if you need to instantiate something there. You could also make it be of Type int[,] and set the integer at a xy position to 0 if no tile is there, and 1 if it is. These are just example, but for all these kinds of tile or voxel terrain, it's common to have a 2- or 3-dimensional array for each tile/voxel that can be accessed at any time.
     
  3. Wombat85

    Wombat85

    Joined:
    Mar 27, 2015
    Posts:
    10
    Yea I figured it out. Basically added a bool then when I created it turned it to true. Added a check before creating if block was already there.