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

always grass on top of dirt

Discussion in 'Scripting' started by Vexer, Jul 9, 2018.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Hey guys im fairly new to perlin noise and i want to make it so that there is always grass on top of the first layer of dirt rn this is my code:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Generator : MonoBehaviour
    7. {
    8.     public Camera FPCamera;
    9.     public GameObject Cube;
    10.     int size = 50;
    11.     public float scale;
    12.     bool enableHeight = true;
    13.     public float scaleModifier;
    14.     public float offSetHeight;
    15.     public Material grass;
    16.     public Material dirt;
    17.     public Material stone;
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Start()
    22.     {
    23.  
    24.         for (var x = 0; x < size; x++)
    25.         {
    26.             for (var z = 0; z < size; z++)
    27.             {
    28.                 var c = Instantiate(Cube, new Vector3(0.5f *x,0,0.5f * z), Quaternion.identity) as GameObject;
    29.  
    30.                 c.transform.parent = transform;
    31.             }
    32.         }
    33.  
    34.         foreach (Transform child in transform)
    35.         {
    36.             var height = Mathf.PerlinNoise(child.transform.position.x / scale, child.transform.position.z / scale);
    37.  
    38.             setMatColor(child, height);
    39.  
    40.             if (enableHeight)
    41.             {
    42.                 applyHeight(child, height);
    43.             }
    44.         }
    45.     }
    46.  
    47.     // Update is called once per frame
    48.     void Update()
    49.     {
    50.         checkMouseClick();
    51.     }
    52.  
    53.     void setMatColor(Transform child, float height)
    54.     {
    55.         child.GetComponent<Renderer>().material = grass;
    56.     }
    57.  
    58.     void applyHeight(Transform child, float height)
    59.     {
    60.         var yValue = Mathf.RoundToInt(height * scaleModifier);
    61.  
    62.         var newVec3 = new Vector3(child.transform.position.x, yValue, child.transform.position.z);
    63.  
    64.         child.transform.position = newVec3;
    65.  
    66.         GameObject clone;
    67.         clone = Instantiate(Cube, new Vector3(child.transform.position.x, child.transform.position.y - .5f, (child.transform.position.z)), Quaternion.identity) as GameObject;
    68.         clone.GetComponent<Renderer>().material = dirt ;
    69.         clone = Instantiate(Cube, new Vector3(child.transform.position.x, child.transform.position.y - 1f, (child.transform.position.z)), Quaternion.identity) as GameObject;
    70.         clone.GetComponent<Renderer>().material = dirt;
    71.         clone = Instantiate(Cube, new Vector3(child.transform.position.x, child.transform.position.y - 1.5f, (child.transform.position.z)), Quaternion.identity) as GameObject;
    72.         clone.GetComponent<Renderer>().material = dirt;
    73.         clone = Instantiate(Cube, new Vector3(child.transform.position.x, child.transform.position.y - 2f, (child.transform.position.z)), Quaternion.identity) as GameObject;
    74.         clone.GetComponent<Renderer>().material = dirt;
    75.         clone = Instantiate(Cube, new Vector3(child.transform.position.x, child.transform.position.y - 2.5f, (child.transform.position.z)), Quaternion.identity) as GameObject;
    76.         clone.GetComponent<Renderer>().material = dirt;
    77.         clone = Instantiate(Cube, new Vector3(child.transform.position.x, child.transform.position.y - 3f, (child.transform.position.z)), Quaternion.identity) as GameObject;
    78.         clone.GetComponent<Renderer>().material = dirt;
    79.     }
    80.  
    81.     void checkMouseClick()
    82.     {
    83.         var ray = FPCamera.ScreenPointToRay(Input.mousePosition);
    84.         RaycastHit hit;
    85.  
    86.         if (Physics.Raycast(ray, out hit, 10f))
    87.         {
    88.             if (Input.GetMouseButtonDown(0))
    89.             {
    90.                 if (hit.transform.tag != "Player")
    91.                 {
    92.                     Destroy(hit.transform.gameObject);
    93.                 }
    94.             }
    95.             else if (Input.GetMouseButtonDown(1))
    96.             {
    97.                 createCube(hit);
    98.             }
    99.         }
    100.     }
    101.  
    102.     void createCube(RaycastHit hit)
    103.     {
    104.         int triIndex = hit.triangleIndex;
    105.         GameObject clone;
    106.  
    107.         Debug.Log(hit.triangleIndex);
    108.  
    109.         switch (triIndex)
    110.         {
    111.             case 0:
    112.             case 1:
    113.                 clone = Instantiate(Cube, new Vector3(hit.transform.position.x, hit.transform.position.y, (hit.transform.position.z + .5f)), Quaternion.identity) as GameObject;
    114.                 return;
    115.             case 2:
    116.             case 3:
    117.                 clone = Instantiate(Cube, new Vector3(hit.transform.position.x, hit.transform.position.y+.5f, (hit.transform.position.z)), Quaternion.identity) as GameObject;
    118.                 return;
    119.             case 4:
    120.             case 5:
    121.                 clone = Instantiate(Cube, new Vector3(hit.transform.position.x, hit.transform.position.y, (hit.transform.position.z -.5f)), Quaternion.identity) as GameObject;
    122.                 return;
    123.             case 6:
    124.             case 7:
    125.                 clone = Instantiate(Cube, new Vector3(hit.transform.position.x, hit.transform.position.y-.5f, (hit.transform.position.z)), Quaternion.identity) as GameObject;
    126.                 return;
    127.             case 8:
    128.             case 9:
    129.                 clone = Instantiate(Cube, new Vector3(hit.transform.position.x-.5f, hit.transform.position.y, (hit.transform.position.z)), Quaternion.identity) as GameObject;
    130.                 return;
    131.             case 10:
    132.             case 11:
    133.                 clone = Instantiate(Cube, new Vector3(hit.transform.position.x+.5f, hit.transform.position.y, (hit.transform.position.z)), Quaternion.identity) as GameObject;
    134.                 return;
    135.             default:
    136.                 return;
    137.         }
    138.     }
    139. }
    140.  
    But the problem with this is that you can see the dirt: https://gyazo.com/2d87a6bb0b8d2cc6341023b09838b804 and i kind of want to make it so you can only see the first grass layer unless you break the grass how can i fix this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    It looks like you are perhaps instantiating multiple overlapping cubes? I see multiple instantiates only apart by 0.5 but the default size of a cube is 1,1,1 so they are overlapping. Maybe your cube is scaled to 1/2 though.

    Not sure what your ultimate goal is here but if you're going for a minecraft game you might wanna see any one of the myriad voxel/minecraft engine tutorials out there for Unity up on Youtube. Making this many GameObjects will certainly NOT be performant enough for realtime rendering once the world approaches any useful size.
     
  3. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Yeah i was about to make a thread that i had too many gameobjects and that my game started to lagg alot thanks for telling me!!
     
    Kurt-Dekker likes this.