Advertisement
shadowplaycoding

P011_CameraController

Apr 29th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraController : MonoBehaviour {
  5.  
  6.     public static CameraController cameraController;
  7.  
  8.     public float panSpeed = 5;
  9.     public float zoomedInAngle = 45;
  10.     public float zoomedOutAngle = 90;
  11.     public float minZoom = 20; // Minimum distance away from XZ plane 0.
  12.     public float maxZoom = 200; // Maximum distance away from XZ plane 0.
  13.     public bool inverseZoom = false;
  14.  
  15.     public static Quaternion currentAngle;
  16.  
  17.     float zoomLevel = 0;
  18.  
  19.     Transform rotationObject;
  20.     Transform zoomObject;
  21.  
  22.     // Used before Start()
  23.     void Awake()
  24.     {
  25.         cameraController = this;
  26.         rotationObject = transform.GetChild(0);
  27.         zoomObject = rotationObject.transform.GetChild(0);
  28.         ResetCamera();
  29.     }
  30.  
  31.     // Update is called once per frame
  32.     void Update() {
  33.  
  34.         ChangeZoom();
  35.         ChangePosition();
  36.  
  37.     }
  38.  
  39.     // This method pans the camera view in the XZ plane
  40.     void ChangePosition()
  41.     {
  42.         if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
  43.         {
  44.             float movementFactor = Mathf.Lerp(minZoom, maxZoom, zoomLevel);
  45.             float distance = panSpeed * Time.deltaTime;
  46.             Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
  47.  
  48.             float dampingFactor = Mathf.Max(Mathf.Abs(Input.GetAxis("Horizontal")), Mathf.Abs(Input.GetAxis("Vertical")));
  49.  
  50.             transform.Translate(distance * dampingFactor * movementFactor * direction);
  51.  
  52.             ClampCameraPan();
  53.  
  54.         }
  55.     }
  56.  
  57.     // This method changes the zoom of the camera
  58.     void ChangeZoom()
  59.     {
  60.         if (Input.GetAxis("Mouse ScrollWheel") != 0)
  61.         {
  62.             if (inverseZoom == false)
  63.             {
  64.                 zoomLevel = Mathf.Clamp01(zoomLevel - Input.GetAxis("Mouse ScrollWheel"));
  65.             }
  66.             else
  67.             {
  68.                 zoomLevel = Mathf.Clamp01(zoomLevel + Input.GetAxis("Mouse ScrollWheel"));
  69.             }
  70.  
  71.             float zoom = Mathf.Lerp(-minZoom, -maxZoom, zoomLevel);
  72.             zoomObject.transform.localPosition = new Vector3(0, 0, zoom);
  73.  
  74.             float zoomAngle = Mathf.Lerp(zoomedInAngle, zoomedOutAngle, zoomLevel);
  75.             rotationObject.transform.localRotation = Quaternion.Euler(zoomAngle, 0, 0);
  76.             currentAngle = rotationObject.transform.localRotation;
  77.  
  78.         }
  79.     }
  80.  
  81.  
  82.     // This method resets the camera to the centre of the scene
  83.     public void ResetCamera()
  84.     {
  85.         this.transform.position = new Vector3(0, 0, 0);
  86.         zoomLevel = 0;
  87.         rotationObject.transform.rotation = Quaternion.Euler(zoomedInAngle, 0, 0);
  88.         currentAngle = rotationObject.transform.rotation;
  89.         zoomObject.transform.localPosition = new Vector3(0, 0, -minZoom);
  90.     }
  91.  
  92.     // This method stops the camera
  93.     void ClampCameraPan()
  94.     {
  95.         Vector3 position = this.transform.position;
  96.  
  97.         if (Galaxy.GalaxyInstance.galaxyView == true)
  98.         {
  99.             position.x = Mathf.Clamp(transform.position.x, -Galaxy.GalaxyInstance.maximumRadius, Galaxy.GalaxyInstance.maximumRadius);
  100.             position.z = Mathf.Clamp(transform.position.z, -Galaxy.GalaxyInstance.maximumRadius, Galaxy.GalaxyInstance.maximumRadius);
  101.         }
  102.         else
  103.         {
  104.             position.x = Mathf.Clamp(transform.position.x, -50, 50);
  105.             position.z = Mathf.Clamp(transform.position.z, -50, 50);
  106.         }
  107.  
  108.         this.transform.position = position;
  109.  
  110.     }
  111.  
  112.     public void MoveTo(Vector3 position)
  113.     {
  114.         this.transform.position = position;
  115.     }
  116.  
  117.     /*
  118.     Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  119.     Removing this comment forfits any rights given to the user under licensing.
  120.     */
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement