Advertisement
Guest User

TP_Camera.cs

a guest
Mar 18th, 2014
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TP_Camera : MonoBehaviour
  5. {
  6.     public static TP_Camera Instance;
  7.  
  8.     public Transform TargetLookAt;
  9.  
  10.     public float Distance = 5f;
  11.     public float DistanceMin = 3f;
  12.     public float DistanceMax = 10f;
  13.     public float DistanceSmooth = 0.05f;
  14.     public float X_MouseSensitivity = 5f;
  15.     public float Y_MouseSensitivity = 5f;
  16.     public float MouseWheelSensitivity = 5f;
  17.     public float X_Smooth = 0.05f;
  18.     public float Y_Smooth = 0.1f;
  19.     public float Y_MinLimit = -40f;
  20.     public float Y_MaxLimit = 80f;
  21.  
  22.  
  23.     private float mouseX = 0f;
  24.     private float mouseY = 0f;
  25.     private float velX = 0f;
  26.     private float velY = 0f;
  27.     private float velZ = 0f;
  28.     private float velDistance = 0f;
  29.     private float startDistance = 0f;
  30.     private Vector3 position = Vector3.zero;
  31.     private Vector3 desiredPosition = Vector3.zero;
  32.     private float desiredDistance = 0f;
  33.  
  34.  
  35.     void Awake()
  36.     {
  37.         Instance = this;
  38.     }
  39.  
  40.     // Use this for initialization
  41.     void Start ()
  42.     {
  43.         Distance = Mathf.Clamp (Distance, DistanceMin, DistanceMax);
  44.         startDistance = Distance;
  45.         Reset ();
  46.  
  47.    
  48.     }
  49.    
  50.     void LateUpdate ()
  51.     {
  52.         if (TargetLookAt == null)
  53.             return;
  54.  
  55.         HandlePlayerInput ();
  56.         CalculateDesiredPosition ();
  57.         UpdatePosition ();
  58.     }
  59.  
  60.     void HandlePlayerInput()
  61.     {
  62.         var deadZone = 0.1f;
  63.  
  64.  
  65.         // If right mouse button is held down and is true
  66.         if (Input.GetMouseButton(1))
  67.         {
  68.             // The RMB is down, get mouse axis input
  69.             mouseX += Input.GetAxis("Mouse X") * X_MouseSensitivity;
  70.             mouseY -= Input.GetAxis("Mouse Y") * Y_MouseSensitivity;
  71.         }
  72.  
  73.         // This is where we will limit mouseY
  74.  
  75.         mouseY = Helper.ClampAngle (mouseY, Y_MinLimit, Y_MaxLimit);
  76.  
  77.         if (Input.GetAxis("Mouse ScrollWheel") < -deadZone || Input.GetAxis("Mouse ScrollWheel") > -deadZone )
  78.         {
  79.             desiredDistance = Mathf.Clamp(Distance - Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity,
  80.                                           DistanceMin, DistanceMax);
  81.  
  82.         }
  83.     }
  84.  
  85.     void CalculateDesiredPosition()
  86.     {
  87.         // Evaluluate distance
  88.         Distance = Mathf.SmoothDamp (Distance, desiredDistance, ref velDistance, DistanceSmooth);
  89.  
  90.         // Calculate desired position
  91.         desiredPosition = CalculatePosition (mouseY, mouseX, Distance);
  92.  
  93.  
  94.     }
  95.  
  96.     Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
  97.     {
  98.         Vector3 direction = new Vector3 (0, 0, -distance);
  99.         Quaternion rotation = Quaternion.Euler (rotationX, rotationY, 0);
  100.         return TargetLookAt.position + rotation * direction;
  101.     }
  102.  
  103.     void UpdatePosition()
  104.     {
  105.         var posX = Mathf.SmoothDamp (position.x, desiredPosition.x, ref velX, X_Smooth);
  106.         var posY = Mathf.SmoothDamp (position.y, desiredPosition.y, ref velY, Y_Smooth);
  107.         var posZ = Mathf.SmoothDamp (position.z, desiredPosition.z, ref velZ, X_Smooth);
  108.         position = new Vector3 (posX, posY, posZ);
  109.  
  110.         transform.position = position;
  111.  
  112.         transform.LookAt (TargetLookAt);
  113.     }
  114.  
  115.     public void Reset ()
  116.     {
  117.         mouseX = 0;
  118.         mouseY = 10;
  119.         Distance = startDistance;
  120.         desiredDistance = Distance;
  121.  
  122.  
  123.     }
  124.  
  125.     public static void UseExistingOrCreateNewMainCamera()
  126.     {
  127.         GameObject tempCamera;
  128.         GameObject targetLookAt;
  129.         TP_Camera myCamera;
  130.  
  131.         if (Camera.mainCamera != null)
  132.         {
  133.             tempCamera = Camera.mainCamera.gameObject; 
  134.         }
  135.         else
  136.         {
  137.             tempCamera = new GameObject("Main Camera");
  138.             tempCamera.AddComponent("Camera");
  139.             tempCamera.tag = "MainCamera";
  140.         }
  141.  
  142.         tempCamera.AddComponent ("TP_Camera");
  143.         myCamera = tempCamera.GetComponent ("TP_Camera") as TP_Camera;
  144.  
  145.         targetLookAt = GameObject.Find("tagetLookAt") as GameObject;
  146.  
  147.         if (targetLookAt == null)
  148.         {
  149.             targetLookAt = new GameObject("targetLookAt");
  150.             targetLookAt.transform.position = Vector3.zero;
  151.         }
  152.  
  153.         myCamera.TargetLookAt = targetLookAt.transform;
  154.  
  155.     }
  156. }
Advertisement
RAW Paste Data Copied
Advertisement