Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class ProceduralMultiTerrain : MonoBehaviour
- {
- public GameObject[] terrainPrefab;
- [SerializeField] private int width = 24;
- [SerializeField] private int height = 32;
- private GameObject[,] terrainList;
- private bool isDirty = true;
- public float offsetX;
- public float offsetY;
- [SerializeField] private float perlinWeight = 0.01f;
- [SerializeField] private float step = 0.2f;
- void Awake()
- {
- Debug.Log( "Setting up Terrain" );
- terrainList = new GameObject[width, height];
- SetTerrainLocAndType();
- }
- void SetTerrainLocAndType()
- {
- for( int x = 0; x < width; x++ )
- {
- for( int y = 0; y < height; y++ )
- {
- float yDiff = y * perlinWeight + offsetX;
- float xDiff = x * perlinWeight + offsetY;
- int prefabSelected = (int)( Mathf.PerlinNoise( xDiff, yDiff ) * terrainPrefab.Length );
- prefabSelected = Mathf.Clamp( prefabSelected, 0, terrainPrefab.Length - 1 );
- float loc = Mathf.Round( Mathf.PerlinNoise( xDiff, yDiff ) * 35 ) ;
- Vector3 pos = transform.position + new Vector3( x, loc, y );
- terrainList[x, y] = Instantiate( terrainPrefab[prefabSelected], pos, transform.rotation, transform );
- terrainList[x, y].name = "Tile " + x + "," + y;
- }
- }
- isDirty = false;
- }
- public float getTerrainHeight( int x, int y )
- {
- return terrainList[x, y].transform.position.y;
- }
- public float getTerrainHeight( Vector2Int loc )
- {
- if( terrainList[loc[0], loc[1]] )
- {
- Debug.Log( terrainList[loc[0], loc[1]].transform.position );
- float y = terrainList[loc[0], loc[1]].transform.position.y;
- return y;
- }
- return -999;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement