SHARE
TWEET

cameraController

a guest Apr 25th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class WorldMapCamera : MonoBehaviour
  6. {
  7.     #region VARIABLES
  8.  
  9.     [Header("MainCamera")]
  10.     [SerializeField] private Camera mainCamera;
  11.  
  12.     [Header("Camera settings")]
  13.     [SerializeField] private float panSpeed = -0.3f;
  14.     [SerializeField] private int maxCameraZoomIn = 3;
  15.     [SerializeField] private float zoomStepSize;
  16.  
  17.     [Header("Camera bounds components")]
  18.     [SerializeField] private Transform worldMapArea;
  19.     [SerializeField] private SpriteRenderer worldMapSpriteRenderer;
  20.  
  21.     #endregion
  22.  
  23.     #region PROPERTIES
  24.  
  25.     private Camera MainCamera { get => mainCamera; }
  26.  
  27.     private float PanSpeed { get => panSpeed; set => panSpeed = value; }
  28.     private int MaxCameraZoomIn { get => maxCameraZoomIn; }
  29.     private float MaxCameraZoomOut { get; set; }
  30.     private float ZoomStepSize { get => zoomStepSize; }
  31.  
  32.     private Transform WorldMapArea { get => worldMapArea; }
  33.     private Sprite WorldMapSprite { get => worldMapSpriteRenderer.sprite; }
  34.  
  35.     private Vector2 AreaSize { get; set; }
  36.  
  37.     private float AreaLeftBound { get; set; }
  38.     private float AreaRightBound { get; set; }
  39.     private float AreaTopBound { get; set; }
  40.     private float AreaBottomBound { get; set; }
  41.  
  42.     #endregion
  43.  
  44.     #region FUNCTIONS
  45.  
  46.     private void Awake ()
  47.     {
  48.         if (MainCamera != null && WorldMapArea != null)
  49.         {
  50.             InitializeCameraSettings();
  51.         }
  52.     }
  53.  
  54.     private void InitializeCameraSettings ()
  55.     {
  56.         CalculateAreaSize();
  57.         RecalculateAreaBounds();
  58.     }
  59.  
  60.     private void CalculateAreaSize ()
  61.     {
  62.         float pixelUnits = CalculateAreaPixelUnits();
  63.  
  64.         AreaSize = new Vector2(WorldMapArea.transform.localScale.x * WorldMapSprite.texture.width / pixelUnits,
  65.                                WorldMapArea.transform.localScale.y * WorldMapSprite.texture.height / pixelUnits);
  66.     }
  67.  
  68.     private float CalculateAreaPixelUnits ()
  69.     {
  70.         return WorldMapSprite.rect.width / WorldMapSprite.bounds.size.x;
  71.     }
  72.  
  73.     private void RecalculateAreaBounds ()
  74.     {
  75.         float verticalExtent = MainCamera.orthographicSize;
  76.         float horizontalExtent = verticalExtent * ((float)Screen.width / (float)Screen.height);
  77.  
  78.         AreaLeftBound = horizontalExtent - AreaSize.x / 2.0f;
  79.         AreaRightBound = AreaSize.x / 2.0f - horizontalExtent;
  80.         AreaBottomBound = verticalExtent - AreaSize.y / 2.0f;
  81.         AreaTopBound = AreaSize.y / 2.0f - verticalExtent;
  82.     }
  83.  
  84.     private void LateUpdate ()
  85.     {
  86.         PanControl();
  87.         ZoomControl();
  88.         CameraClamp();
  89.     }
  90.  
  91.     private void PanControl ()
  92.     {
  93.         if (Input.GetMouseButton(0) == true)
  94.         {
  95.             float x = Input.GetAxis("Mouse X") * panSpeed;
  96.             float y = Input.GetAxis("Mouse Y") * panSpeed;
  97.  
  98.             transform.Translate(x, y, 0);
  99.         }
  100.     }
  101.  
  102.     private void ZoomControl ()
  103.     {
  104.         if ((Input.GetAxis("Mouse ScrollWheel") != 0))
  105.         {
  106.             if ((Input.GetAxis("Mouse ScrollWheel") > 0) && MainCamera.orthographicSize > MaxCameraZoomIn)
  107.             {
  108.                 MainCamera.orthographicSize = MainCamera.orthographicSize - ZoomStepSize;
  109.             }
  110.  
  111.             CalculateMaximumZoomOut();
  112.  
  113.             if ((Input.GetAxis("Mouse ScrollWheel") < 0) && MainCamera.orthographicSize < MaxCameraZoomOut)
  114.             {
  115.                 MainCamera.orthographicSize = MainCamera.orthographicSize + ZoomStepSize;
  116.             }
  117.  
  118.             RecalculateAreaBounds();
  119.         }
  120.     }
  121.  
  122.     private void CalculateMaximumZoomOut ()
  123.     {
  124.         float w = Screen.width / AreaSize.x;
  125.         float h = Screen.height / AreaSize.y;
  126.         float ratio = w / h;
  127.         float ratio2 = h / w;
  128.  
  129.         if (ratio2 < ratio)
  130.         {
  131.             // Round to the set up MaxCameraZoomOut
  132.             MaxCameraZoomOut = AreaSize.y / 2;
  133.             MaxCameraZoomOut = System.Convert.ToInt32(System.Math.Floor(MaxCameraZoomOut / ratio));
  134.         }
  135.     }
  136.  
  137.     private void CameraClamp ()
  138.     {
  139.         Vector3 cameraPosition = transform.position;
  140.         cameraPosition.x = Mathf.Clamp(cameraPosition.x, AreaLeftBound, AreaRightBound);
  141.         cameraPosition.y = Mathf.Clamp(cameraPosition.y, AreaBottomBound, AreaTopBound);
  142.         transform.position = cameraPosition;
  143.     }
  144.  
  145.     #endregion
  146. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top