ConstrainedCamera.cs 874 B

123456789101112131415161718192021222324252627
  1. /*****************************************************************************
  2. * Constrained Camera created by Mitch Thompson
  3. * Full irrevocable rights and permissions granted to Esoteric Software
  4. *****************************************************************************/
  5. using UnityEngine;
  6. using System.Collections;
  7. public class ConstrainedCamera : MonoBehaviour {
  8. public Transform target;
  9. public Vector3 offset;
  10. public Vector3 min;
  11. public Vector3 max;
  12. public float smoothing = 5f;
  13. // Update is called once per frame
  14. void LateUpdate () {
  15. Vector3 goalPoint = target.position + offset;
  16. goalPoint.x = Mathf.Clamp(goalPoint.x, min.x, max.x);
  17. goalPoint.y = Mathf.Clamp(goalPoint.y, min.y, max.y);
  18. goalPoint.z = Mathf.Clamp(goalPoint.z, min.z, max.z);
  19. transform.position = Vector3.Lerp(transform.position, goalPoint, smoothing * Time.deltaTime);
  20. }
  21. }