Advertisement
EdibleCookie

CameraControls

Jul 23rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.76 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. [AddComponentMenu("Camera-Control/3dsMax Camera Style")]
  6. public class cameraControls : MonoBehaviour
  7. {
  8.     public Transform target;
  9.     public Vector3 targetOffset;
  10.     public float distance = 5.0f;
  11.     public float maxDistance = 20;
  12.     public float minDistance = .6f;
  13.     public float xSpeed = 200.0f;
  14.     public float ySpeed = 200.0f;
  15.     public int yMinLimit = -80;
  16.     public int yMaxLimit = 80;
  17.     public int zoomRate = 40;
  18.     public float panSpeed = 0.3f;
  19.     public float zoomDampening = 5.0f;
  20.  
  21.     private float xDeg = 0.0f;
  22.     private float yDeg = 0.0f;
  23.     private float currentDistance;
  24.     private float desiredDistance;
  25.     private Quaternion currentRotation;
  26.     private Quaternion desiredRotation;
  27.     private Quaternion rotation;
  28.     private Vector3 position;
  29.  
  30.     void Start() { Init(); }
  31.     void OnEnable() { Init(); }
  32.  
  33.     public void Init()
  34.     {
  35.         //If there is no target, create a temporary target at 'distance' from the cameras current viewpoint
  36.         if (!target)
  37.         {
  38.             GameObject go = new GameObject("Cam Target");
  39.             go.transform.position = transform.position + (transform.forward * distance);
  40.             target = go.transform;
  41.         }
  42.  
  43.         distance = Vector3.Distance(transform.position, target.position);
  44.         currentDistance = distance;
  45.         desiredDistance = distance;
  46.  
  47.         //be sure to grab the current rotations as starting points.
  48.         position = transform.position;
  49.         rotation = transform.rotation;
  50.         currentRotation = transform.rotation;
  51.         desiredRotation = transform.rotation;
  52.  
  53.         xDeg = Vector3.Angle(Vector3.right, transform.right);
  54.         yDeg = Vector3.Angle(Vector3.up, transform.up);
  55.     }
  56.  
  57.     /*
  58.      * Camera logic on LateUpdate to only update after all character movement logic has been handled.
  59.      */
  60.     void LateUpdate()
  61.     {
  62.         // If middle mouse is selected, we pan by way of transforming the target in screenspace? PAN!
  63.         if (Input.GetMouseButton(2))
  64.         {
  65.             //grab the rotation of the camera so we can move in a psuedo local XY space
  66.             target.rotation = transform.rotation;
  67.             target.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panSpeed);
  68.             target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);
  69.         }
  70.         // If LMB and Control is selected, we pan by way of transforming the target in screenspace? PAN!
  71.         if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.LeftControl))
  72.         {
  73.             //grab the rotation of the camera so we can move in a psuedo local XY space
  74.             target.rotation = transform.rotation;
  75.             target.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panSpeed);
  76.             target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);
  77.         }
  78.         // If LMB and Shift? ZOOM!
  79.         else if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.LeftShift))
  80.         {
  81.             desiredDistance -= Input.GetAxis("Mouse Y") * Time.deltaTime * zoomRate * 0.125f * Mathf.Abs(desiredDistance);
  82.         }
  83.         // If LMB? ORBIT!
  84.         else if (Input.GetMouseButton(1))
  85.         {
  86.             xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
  87.             yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
  88.  
  89.             ////////OrbitAngle
  90.  
  91.             //Clamp the vertical axis for the orbit
  92.             yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
  93.             // set camera rotation
  94.             desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
  95.             currentRotation = transform.rotation;
  96.  
  97.             rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
  98.             transform.rotation = rotation;
  99.         }
  100.  
  101.         ////////Orbit Position
  102.  
  103.         // affect the desired Zoom distance if we roll the scrollwheel
  104.         desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
  105.         //clamp the zoom min/max
  106.         desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
  107.         // For smoothing of the zoom, lerp distance
  108.         currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
  109.  
  110.         // calculate position based on the new currentDistance
  111.         position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);
  112.         transform.position = position;
  113.     }
  114.  
  115.     private static float ClampAngle(float angle, float min, float max)
  116.     {
  117.         if (angle < -360)
  118.             angle += 360;
  119.         if (angle > 360)
  120.             angle -= 360;
  121.         return Mathf.Clamp(angle, min, max);
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement