Guest User

Generate Terrain

a guest
Oct 5th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class GenerateTerrain : MonoBehaviour {
  6.  
  7. public int heightScale = 5;
  8. public float detailScale = 5.0f;
  9. List<GameObject> myRocks = new List<GameObject>();
  10.  
  11. void Start ()
  12. {
  13. Mesh mesh = this.GetComponent<MeshFilter> ().mesh;
  14. Vector3[] vertices = mesh.vertices;
  15. for (int v = 0; v < vertices.Length; v++)
  16. {
  17. vertices [v].y = Mathf.PerlinNoise ((vertices [v].x + this.transform.position.x) / detailScale,
  18. (vertices [v].z + this.transform.position.z) / detailScale) * heightScale;
  19.  
  20. if (vertices [v].y > 0.8)
  21. {
  22. GameObject newRock = RockPool.getRock ();
  23. if (newRock != null)
  24. {
  25. Vector3 rockPos = new Vector3 (vertices [v].x + this.transform.position.x,
  26. vertices [v].y + 0.5f, //Reduce on y axis if object is floating above terrain
  27. vertices [v].z + this.transform.position.z);
  28.  
  29. newRock.transform.position = rockPos;
  30. newRock.SetActive (true);
  31. myRocks.Add (newRock);
  32. }
  33. }
  34.  
  35. }
  36.  
  37. mesh.vertices = vertices;
  38. mesh.RecalculateBounds ();
  39. mesh.RecalculateNormals ();
  40. this.gameObject.AddComponent<MeshCollider> ();
  41. }
  42.  
  43.  
  44. void OnDestroy()
  45. {
  46. for (int i = 0; i < myRocks.Count; i++)
  47. {
  48. if (myRocks [i] != null)
  49. myRocks [i].SetActive (false);
  50. }
  51. myRocks.Clear ();
  52. }
  53.  
  54.  
  55. void Update ()
  56. {
  57.  
  58. }
  59. }
Add Comment
Please, Sign In to add comment