Enviroment.cs 935 B

12345678910111213141516171819202122232425262728293031323334
  1. using UnityEngine;
  2. public class Enviroment : MonoBehaviour
  3. {
  4. public float envPosX;
  5. public float envPosY;
  6. public float envPosZ;
  7. public float maxCheckDistance = 100f;
  8. public LayerMask groundLayer;
  9. public float groundOffset = 3f;
  10. // Start is called once before the first execution of Update after the MonoBehaviour is created
  11. void Start()
  12. {
  13. groundLayer = LayerMask.GetMask("Ground");
  14. SnapToGround();
  15. envPosX = transform.position.x;
  16. envPosY = transform.position.y;
  17. envPosZ = transform.position.z;
  18. }
  19. // Update is called once per frame
  20. void SnapToGround()
  21. {
  22. RaycastHit hit;
  23. if (Physics.Raycast(transform.position, Vector3.down, out hit, maxCheckDistance, groundLayer))
  24. {
  25. Vector3 pos = transform.position;
  26. pos.y = hit.point.y + groundOffset;
  27. transform.position = pos;
  28. }
  29. }
  30. }