Search Unity

Guys i really need help with generating caves in terrain

Discussion in '2D' started by Lamon112, Apr 22, 2017.

  1. Lamon112

    Lamon112

    Joined:
    Feb 8, 2017
    Posts:
    3
    I want to add Caves so if annyone could help me i really need it.


    code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class GenerateChunk : MonoBehaviour {
    public GameObject grassBlock;
    public GameObject dirtBlock;
    public GameObject stoneBlock;
    public int smoothnes;
    public int additionalHight;
    public float multiplayerHight;
    public int width;
    float seed;
    public GameObject tileCoal;
    public GameObject tileDiamond;
    public GameObject tileGold;
    public GameObject tileIron;
    public GameObject tileSchwantz;
    public GameObject tileLarp;
    public GameObject tree;
    public float chanceSchwantz;
    public float chanceLarp;
    public float chanceCoal;
    public float chanceDiamond;
    public float chanceGold;
    public float chanceIron;
    float treeCount;
    // Use this for initialization
    void Start () {
    seed = Random.Range(-1000, 1000);
    Generate();
    }

    void Generate()
    {
    for (int i = 0; i < width; i++)
    {

    int h = Mathf.RoundToInt(Mathf.PerlinNoise(seed, (i + transform.position.x) / smoothnes) * multiplayerHight) + additionalHight;
    for (int j = 0; j < h; j++)
    {
    GameObject selectedTile;
    if(j < h - 4 )
    {
    selectedTile = stoneBlock;
    }
    else if(j < h - 1 )
    {
    selectedTile = dirtBlock;
    }
    else
    {
    selectedTile = grassBlock;
    }
    GameObject newTile = Instantiate(selectedTile, Vector3.zero, Quaternion.identity) as GameObject;
    if (treeCount < 4)
    {
    GameObject treeTile = Instantiate(tree, Vector3.zero, Quaternion.identity) as GameObject;
    treeTile.transform.parent = this.gameObject.transform;
    treeTile.transform.localPosition = new Vector3(i + Random.Range(0,65), h + Random.Range(3,4));

    treeCount++;
    }
    newTile.transform.parent = this.gameObject.transform;
    newTile.transform.localPosition = new Vector3(i, j);

    }

    }
    Populate();
    }
    public void Populate()
    {
    foreach(GameObject t in GameObject.FindGameObjectsWithTag("Tile Stone"))
    {
    if(t.transform.parent = this.gameObject.transform)
    {

    float r = Random.Range(0f, 100f);
    GameObject selectedTile = null;

    if(chanceLarp > r )
    {
    selectedTile = tileLarp;
    }

    else if (chanceDiamond > r)
    {

    selectedTile = tileDiamond;
    }
    else if (chanceSchwantz > r)
    {

    selectedTile = tileSchwantz;
    }
    else if (chanceGold > r)
    {
    Debug.Log(r);
    selectedTile = tileGold;
    }
    else if (chanceIron > r)
    {
    Debug.Log(r);
    selectedTile = tileIron;
    }
    else if (chanceCoal > r)
    {
    Debug.Log(r);
    selectedTile = tileCoal;
    }
    if (selectedTile != null)
    {
    GameObject NewResourceTile = Instantiate(selectedTile, t.transform.position, Quaternion.identity) as GameObject;
    NewResourceTile.transform.parent = transform;
    Destroy(t);
    }
    }
    }
    }
    }