Advertisement
shadowplaycoding

P008_CameraController

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