Advertisement
Guest User

maxCamera

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