Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class WorldMapCamera : MonoBehaviour
- {
- #region VARIABLES
- [Header("MainCamera")]
- [SerializeField] private Camera mainCamera;
- [Header("Camera settings")]
- [SerializeField] private float panSpeed = -0.3f;
- [SerializeField] private int maxCameraZoomIn = 3;
- [SerializeField] private float zoomStepSize;
- [Header("Camera bounds components")]
- [SerializeField] private Transform worldMapArea;
- [SerializeField] private SpriteRenderer worldMapSpriteRenderer;
- #endregion
- #region PROPERTIES
- private Camera MainCamera { get => mainCamera; }
- private float PanSpeed { get => panSpeed; set => panSpeed = value; }
- private int MaxCameraZoomIn { get => maxCameraZoomIn; }
- private float MaxCameraZoomOut { get; set; }
- private float ZoomStepSize { get => zoomStepSize; }
- private Transform WorldMapArea { get => worldMapArea; }
- private Sprite WorldMapSprite { get => worldMapSpriteRenderer.sprite; }
- private Vector2 AreaSize { get; set; }
- private float AreaLeftBound { get; set; }
- private float AreaRightBound { get; set; }
- private float AreaTopBound { get; set; }
- private float AreaBottomBound { get; set; }
- #endregion
- #region FUNCTIONS
- private void Awake ()
- {
- if (MainCamera != null && WorldMapArea != null)
- {
- InitializeCameraSettings();
- }
- }
- private void InitializeCameraSettings ()
- {
- CalculateAreaSize();
- RecalculateAreaBounds();
- }
- private void CalculateAreaSize ()
- {
- float pixelUnits = CalculateAreaPixelUnits();
- AreaSize = new Vector2(WorldMapArea.transform.localScale.x * WorldMapSprite.texture.width / pixelUnits,
- WorldMapArea.transform.localScale.y * WorldMapSprite.texture.height / pixelUnits);
- }
- private float CalculateAreaPixelUnits ()
- {
- return WorldMapSprite.rect.width / WorldMapSprite.bounds.size.x;
- }
- private void RecalculateAreaBounds ()
- {
- float verticalExtent = MainCamera.orthographicSize;
- float horizontalExtent = verticalExtent * ((float)Screen.width / (float)Screen.height);
- AreaLeftBound = horizontalExtent - AreaSize.x / 2.0f;
- AreaRightBound = AreaSize.x / 2.0f - horizontalExtent;
- AreaBottomBound = verticalExtent - AreaSize.y / 2.0f;
- AreaTopBound = AreaSize.y / 2.0f - verticalExtent;
- }
- private void LateUpdate ()
- {
- PanControl();
- ZoomControl();
- CameraClamp();
- }
- private void PanControl ()
- {
- if (Input.GetMouseButton(0) == true)
- {
- float x = Input.GetAxis("Mouse X") * panSpeed;
- float y = Input.GetAxis("Mouse Y") * panSpeed;
- transform.Translate(x, y, 0);
- }
- }
- private void ZoomControl ()
- {
- if ((Input.GetAxis("Mouse ScrollWheel") != 0))
- {
- if ((Input.GetAxis("Mouse ScrollWheel") > 0) && MainCamera.orthographicSize > MaxCameraZoomIn)
- {
- MainCamera.orthographicSize = MainCamera.orthographicSize - ZoomStepSize;
- }
- CalculateMaximumZoomOut();
- if ((Input.GetAxis("Mouse ScrollWheel") < 0) && MainCamera.orthographicSize < MaxCameraZoomOut)
- {
- MainCamera.orthographicSize = MainCamera.orthographicSize + ZoomStepSize;
- }
- RecalculateAreaBounds();
- }
- }
- private void CalculateMaximumZoomOut ()
- {
- float w = Screen.width / AreaSize.x;
- float h = Screen.height / AreaSize.y;
- float ratio = w / h;
- float ratio2 = h / w;
- if (ratio2 < ratio)
- {
- // Round to the set up MaxCameraZoomOut
- MaxCameraZoomOut = AreaSize.y / 2;
- MaxCameraZoomOut = System.Convert.ToInt32(System.Math.Floor(MaxCameraZoomOut / ratio));
- }
- }
- private void CameraClamp ()
- {
- Vector3 cameraPosition = transform.position;
- cameraPosition.x = Mathf.Clamp(cameraPosition.x, AreaLeftBound, AreaRightBound);
- cameraPosition.y = Mathf.Clamp(cameraPosition.y, AreaBottomBound, AreaTopBound);
- transform.position = cameraPosition;
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement