Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class GenerateTerrain : MonoBehaviour {
- public int heightScale = 5;
- public float detailScale = 5.0f;
- List<GameObject> myRocks = new List<GameObject>();
- void Start ()
- {
- Mesh mesh = this.GetComponent<MeshFilter> ().mesh;
- Vector3[] vertices = mesh.vertices;
- for (int v = 0; v < vertices.Length; v++)
- {
- vertices [v].y = Mathf.PerlinNoise ((vertices [v].x + this.transform.position.x) / detailScale,
- (vertices [v].z + this.transform.position.z) / detailScale) * heightScale;
- if (vertices [v].y > 0.8)
- {
- GameObject newRock = RockPool.getRock ();
- if (newRock != null)
- {
- Vector3 rockPos = new Vector3 (vertices [v].x + this.transform.position.x,
- vertices [v].y + 0.5f, //Reduce on y axis if object is floating above terrain
- vertices [v].z + this.transform.position.z);
- newRock.transform.position = rockPos;
- newRock.SetActive (true);
- myRocks.Add (newRock);
- }
- }
- }
- mesh.vertices = vertices;
- mesh.RecalculateBounds ();
- mesh.RecalculateNormals ();
- this.gameObject.AddComponent<MeshCollider> ();
- }
- void OnDestroy()
- {
- for (int i = 0; i < myRocks.Count; i++)
- {
- if (myRocks [i] != null)
- myRocks [i].SetActive (false);
- }
- myRocks.Clear ();
- }
- void Update ()
- {
- }
- }
Add Comment
Please, Sign In to add comment