Search Unity

Camera boundaries

Discussion in 'Scripting' started by Ericks89, Sep 17, 2017.

  1. Ericks89

    Ericks89

    Joined:
    Nov 3, 2012
    Posts:
    39
    I'm trying to set boundaries so the the camera doesn't show outside of the tilemap.
    My code for the camera is below.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraFollow : MonoBehaviour {
    4.     public Transform target;
    5.     public float camSPD = 0.125f;
    6.     public float xMin = -1;
    7.     public float xMax = 1;
    8.     public float yMin = -1;
    9.     public float yMax = 1;
    10.  
    11.     Transform t;
    12.  
    13.     void Awake () {
    14.         t = transform;
    15.     }
    16.  
    17.     void LateUpdate () {
    18.         float x = Mathf.Clamp(target.position.x, xMin, xMax);
    19.         float y = Mathf.Clamp(target.position.y, yMin, yMax);
    20.  
    21.         t.position = new Vector3(x, y, t.position.z);
    22.  
    23.         Vector3 desiredPosition = t.position = target.position;
    24.         Vector3 camPosition = Vector3.Lerp(t.position,desiredPosition,camSPD*Time.deltaTime);
    25.      
    26.         transform.position = camPosition;
    27.      
    28.     }
    29. }
    Any help is appreciated

    Edit: Solved it :)
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraFollow : MonoBehaviour {
    4.     public Transform target;
    5.  
    6.     Vector3 velocity = Vector3.zero;
    7.  
    8.     public float camSpeed = 0.15f;
    9.  
    10.     public bool xMinEnabled = false;
    11.     public float xMin = 0;
    12.     public bool xMaxEnabled = false;
    13.     public float xMax = 0;
    14.     public bool yMinEnabled = false;
    15.     public float yMin = 0;
    16.     public bool yMaxEnabled = false;
    17.     public float yMax = 0;
    18.  
    19.     void FixedUpdate () {
    20.         Vector3 targetPos = target.position;
    21.  
    22.         // Horizontal Boundaries
    23.         if (xMinEnabled && xMaxEnabled) {
    24.             targetPos.x = Mathf.Clamp(target.position.x, xMin, xMax);
    25.         } else if (xMinEnabled) {
    26.             targetPos.x = Mathf.Clamp(target.position.x, xMin, target.position.x);
    27.         } else if (xMaxEnabled) {
    28.             targetPos.x = Mathf.Clamp(target.position.x, target.position.x, xMax);
    29.         }
    30.  
    31.         // Vertical Boundaries
    32.         if (yMinEnabled && yMaxEnabled) {
    33.             targetPos.y = Mathf.Clamp(target.position.y, yMin, yMax);
    34.         } else if (yMinEnabled) {
    35.             targetPos.y = Mathf.Clamp(target.position.y, yMin, target.position.y);
    36.         } else if (yMaxEnabled) {
    37.             targetPos.y = Mathf.Clamp(target.position.y, target.position.y, yMax);
    38.         }
    39.  
    40.         targetPos.z = transform.position.z;
    41.         transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocity, camSpeed);
    42.     }
    43. }
     
    Last edited: Sep 17, 2017