Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ProceduralMultiTerrain : 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 perlinWeight = 0.01f;
  16.     [SerializeField] private float step = 0.2f;
  17.     void Awake()
  18.     {
  19.         Debug.Log( "Setting up Terrain" );
  20.         terrainList = new GameObject[width, height];
  21.         SetTerrainLocAndType();
  22.  
  23.     }
  24.  
  25.     void SetTerrainLocAndType()
  26.     {
  27.         for( int x = 0; x < width; x++ )
  28.         {
  29.             for( int y = 0; y < height; y++ )
  30.             {
  31.                 float yDiff = y * perlinWeight + offsetX;
  32.                 float xDiff = x * perlinWeight + offsetY;
  33.  
  34.                 int prefabSelected = (int)( Mathf.PerlinNoise( xDiff, yDiff ) * terrainPrefab.Length );
  35.                 prefabSelected = Mathf.Clamp( prefabSelected, 0, terrainPrefab.Length - 1 );
  36.  
  37.                 float loc = Mathf.Round( Mathf.PerlinNoise( xDiff, yDiff ) * 35 ) ;
  38.  
  39.                 Vector3 pos = transform.position + new Vector3( x, loc, y );
  40.  
  41.                 terrainList[x, y] = Instantiate( terrainPrefab[prefabSelected], pos, transform.rotation, transform );
  42.                 terrainList[x, y].name = "Tile " + x + "," + y;
  43.  
  44.             }
  45.         }
  46.         isDirty = false;
  47.     }
  48.  
  49.     public float getTerrainHeight( int x, int y )
  50.     {
  51.         return terrainList[x, y].transform.position.y;
  52.     }
  53.     public float getTerrainHeight( Vector2Int loc )
  54.     {
  55.         if( terrainList[loc[0], loc[1]] )
  56.         {
  57.             Debug.Log( terrainList[loc[0], loc[1]].transform.position );
  58.             float y = terrainList[loc[0], loc[1]].transform.position.y;
  59.             return y;
  60.         }
  61.         return -999;
  62.  
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement