Advertisement
Guest User

Untitled

a guest
Nov 26th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.20 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. namespace RootMotion {
  5.  
  6.     /// <summary>
  7.     /// 3rd person camera controller.
  8.     /// </summary>
  9.     public class CameraController : MonoBehaviour {
  10.  
  11.         // When to update the camera?
  12.         [System.Serializable]
  13.         public enum UpdateMode {
  14.             Update,
  15.             FixedUpdate,
  16.             LateUpdate
  17.         }
  18.  
  19.         public Transform target; // The target Transform to follow
  20.         public Transform rotationSpace; // If assigned, will use this Transform's rotation as the rotation space instead of the world space. Useful with spherical planets.
  21.         public UpdateMode updateMode = UpdateMode.LateUpdate; // When to update the camera?
  22.         public bool lockCursor = true; // If true, the mouse will be locked to screen center and hidden
  23.         public bool smoothFollow; // If > 0, camera will smoothly interpolate towards the target
  24.         public float followSpeed = 10f; // Smooth follow speed
  25.         public float distance = 10.0f; // The current distance to target
  26.         public float minDistance = 4; // The minimum distance to target
  27.         public float maxDistance = 10; // The maximum distance to target
  28.         public float zoomSpeed = 10f; // The speed of interpolating the distance
  29.         public float zoomSensitivity = 1f; // The sensitivity of mouse zoom
  30.         public float rotationSensitivity = 3.5f; // The sensitivity of rotation
  31.         public float yMinLimit = -20; // Min vertical angle
  32.         public float yMaxLimit = 80; // Max vertical angle
  33.         public Vector3 offset = new Vector3(0, 1.5f, 0.5f); // The offset from target relative to camera rotation
  34.         public bool rotateAlways = true; // Always rotate to mouse?
  35.         public bool rotateOnLeftButton; // Rotate to mouse when left button is pressed?
  36.         public bool rotateOnRightButton; // Rotate to mouse when right button is pressed?
  37.         public bool rotateOnMiddleButton; // Rotate to mouse when middle button is pressed?
  38.        
  39.         public float x { get; private set; } // The current x rotation of the camera
  40.         public float y { get; private set; } // The current y rotation of the camera
  41.         public float distanceTarget { get; private set; } // Get/set distance
  42.  
  43.         private Vector3 targetDistance, position;
  44.         private Quaternion rotation = Quaternion.identity;
  45.         private Vector3 smoothPosition;
  46.         private Camera cam;
  47.  
  48.         // Initiate, set the params to the current transformation of the camera relative to the target
  49.         protected virtual void Awake () {
  50.             Vector3 angles = transform.eulerAngles;
  51.             x = angles.y;
  52.             y = angles.x;
  53.            
  54.             distanceTarget = distance;
  55.             smoothPosition = transform.position;
  56.  
  57.             cam = GetComponent<Camera>();
  58.  
  59.             lastUp = rotationSpace != null? rotationSpace.up: Vector3.up;
  60.         }
  61.  
  62.         protected virtual void Update() {
  63.             if (updateMode == UpdateMode.Update) UpdateTransform();
  64.         }
  65.  
  66.         protected virtual void FixedUpdate() {
  67.             if (updateMode == UpdateMode.FixedUpdate) UpdateTransform();
  68.         }
  69.  
  70.         protected virtual void LateUpdate() {
  71.             UpdateInput();
  72.  
  73.             if (updateMode == UpdateMode.LateUpdate) UpdateTransform();
  74.         }
  75.  
  76.         // Read the user input
  77.         public void UpdateInput() {
  78.             if (target == null || !cam.enabled) return;
  79.  
  80.             // Cursors
  81.             Cursor.lockState = lockCursor? CursorLockMode.Locked: CursorLockMode.None;
  82.             Cursor.visible = lockCursor? false: true;
  83.  
  84.             // Should we rotate the camera?
  85.             bool rotate = rotateAlways || (rotateOnLeftButton && Input.GetMouseButton(0)) || (rotateOnRightButton && Input.GetMouseButton(1)) || (rotateOnMiddleButton && Input.GetMouseButton(2));
  86.  
  87.             // delta rotation
  88.             if (rotate) {
  89.                 x += Input.GetAxis("Mouse X") * rotationSensitivity;
  90.                 y = ClampAngle(y - Input.GetAxis("Mouse Y") * rotationSensitivity, yMinLimit, yMaxLimit);
  91.             }
  92.  
  93.             // Distance
  94.             distanceTarget = Mathf.Clamp(distanceTarget + zoomAdd, minDistance, maxDistance);
  95.         }
  96.  
  97.         // Update the camera transform
  98.         public void UpdateTransform() {
  99.             UpdateTransform(Time.deltaTime);
  100.         }
  101.  
  102.         private Quaternion r = Quaternion.identity;
  103.         private Vector3 lastUp;
  104.  
  105.         public void UpdateTransform(float deltaTime) {
  106.             if (target == null || !cam.enabled) return;
  107.            
  108.             // Distance
  109.             distance += (distanceTarget - distance) * zoomSpeed * deltaTime;
  110.            
  111.             // Rotation
  112.             rotation = Quaternion.AngleAxis(x, Vector3.up) * Quaternion.AngleAxis(y, Vector3.right);
  113.  
  114.  
  115.             if (rotationSpace != null) {
  116.                 r = Quaternion.FromToRotation(lastUp, rotationSpace.up) * r;
  117.                 rotation = r * rotation;
  118.  
  119.                 lastUp = rotationSpace.up;
  120.  
  121.             }
  122.  
  123.  
  124.             // Smooth follow
  125.             if (!smoothFollow) smoothPosition = target.position;
  126.             else smoothPosition = Vector3.Lerp(smoothPosition, target.position, deltaTime * followSpeed);
  127.  
  128.             // Position
  129.             position = smoothPosition + rotation * (offset - Vector3.forward * distance);
  130.            
  131.             // Translating the camera
  132.             transform.position = position;
  133.             transform.rotation = rotation;
  134.         }
  135.  
  136.         // Zoom input
  137.         private float zoomAdd {
  138.             get {
  139.                 float scrollAxis = Input.GetAxis("Mouse ScrollWheel");
  140.                 if (scrollAxis > 0) return -zoomSensitivity;
  141.                 if (scrollAxis < 0) return zoomSensitivity;
  142.                 return 0;
  143.             }
  144.         }
  145.  
  146.         // Clamping Euler angles
  147.         private float ClampAngle (float angle, float min, float max) {
  148.             if (angle < -360) angle += 360;
  149.             if (angle > 360) angle -= 360;
  150.             return Mathf.Clamp (angle, min, max);
  151.         }
  152.        
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement