Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ProceduralGridTerrain : MonoBehaviour
  6. {
  7.  
  8.     public GameObject terrainPrefab;
  9.     [SerializeField] private int width = 24;
  10.     [SerializeField] private int height = 32;
  11.     private GameObject[,] terrainList;
  12.     private bool isDirty = true;
  13.     public float offsetX;
  14.     public float offsetY;
  15.     [SerializeField] private float step = 0.2f;
  16.     void Awake()
  17.     {
  18.         Debug.Log( "Setting up Terrain" );
  19.         terrainList = new GameObject[width, height];
  20.         for( int x = 0; x < width; x++ )
  21.         {
  22.             for( int y = 0; y < height; y++ )
  23.             {
  24.                 //Debug.Log( x + ", " + y );
  25.                 Vector3 pos = transform.position + new Vector3( x, 0, y );
  26.                 terrainList[x, y] = Instantiate( terrainPrefab, pos, transform.rotation, transform );
  27.                 terrainList[x, y].name = "Tile " + x + "," + y;
  28.             }
  29.         }
  30.         //UpdateTerrainLoc();
  31.     }
  32.  
  33.     // Update is called once per frame
  34.     void Update()
  35.     {
  36.  
  37.         if( Input.GetKeyDown( KeyCode.U ) )
  38.         {
  39.             offsetY += step;
  40.             isDirty = true;
  41.         }
  42.  
  43.         if( Input.GetKeyDown( KeyCode.H ) )
  44.         {
  45.             offsetX -= step;
  46.             isDirty = true;
  47.         }
  48.  
  49.         if( Input.GetKeyDown( KeyCode.J ) )
  50.         {
  51.             offsetY -= step;
  52.             isDirty = true;
  53.         }
  54.  
  55.         if( Input.GetKeyDown( KeyCode.K ) )
  56.         {
  57.             offsetX += step;
  58.             isDirty = true;
  59.         }
  60.         if( isDirty )
  61.             UpdateTerrainLoc();
  62.  
  63.     }
  64.  
  65.     void UpdateTerrainLoc()
  66.     {
  67.         for( int x = 0; x < width; x++ )
  68.         {
  69.             for( int y = 0; y < height; y++ )
  70.             {
  71.                 float xDiff = x * .01f + offsetX;
  72.                 float yDiff = y * .01f + offsetY;
  73.  
  74.                
  75.  
  76.                 float loc = Mathf.Round( Mathf.PerlinNoise( xDiff, yDiff ) * 20 );
  77.                 Debug.Log( xDiff + ", " + yDiff + ", " + loc);
  78.                 Vector3 pos = terrainList[x, y].transform.position;
  79.                 pos.y = loc;
  80.                 terrainList[x, y].transform.position = pos;
  81.             }
  82.         }
  83.         isDirty = false;
  84.     }
  85.  
  86.     public float getTerrainHeight( int x, int y )
  87.     {
  88.         return terrainList[x, y].transform.position.y;
  89.     }
  90.     public float getTerrainHeight( Vector2Int loc )
  91.     {
  92.         if( terrainList[loc[0], loc[1]] )
  93.         {
  94.             Debug.Log( terrainList[loc[0], loc[1]].transform.position );
  95.             float y = terrainList[loc[0], loc[1]].transform.position.y;
  96.             return y;
  97.         }
  98.         return -999;
  99.  
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement