TC_LevelWithTerrain.cs 940 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. [ExecuteInEditMode]
  5. public class TC_LevelWithTerrain : MonoBehaviour {
  6. public bool levelChildren;
  7. void Update()
  8. {
  9. if (levelChildren)
  10. {
  11. levelChildren = false;
  12. LevelChildren();
  13. }
  14. }
  15. void LevelChildren()
  16. {
  17. Transform child;
  18. RaycastHit hit;
  19. Ray ray = new Ray();
  20. ray.direction = new Vector3(0, -1, 0);
  21. int layer = LayerMask.NameToLayer("Terrain");
  22. layer = ~layer;
  23. int childCount = transform.childCount;
  24. for (int i = 0; i < childCount; i++)
  25. {
  26. child = transform.GetChild(i);
  27. ray.origin = child.position;
  28. if (Physics.Raycast(ray, out hit))
  29. {
  30. child.position = new Vector3(child.position.x, hit.point.y, child.position.z);
  31. }
  32. }
  33. }
  34. }