Advertisement
napland

CameraRotator

May 3rd, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public enum Orientation
  5. {
  6.     up,down,left,right,forward,back
  7. }
  8.  
  9.  
  10. public class CameraRotator : MonoBehaviour
  11. {
  12.     public float speed = 90f;
  13.     public bool gravityOn = true;
  14.     public float gravScale = -100f;
  15.     public bool debugOn = false;
  16.     public float minSqrDist = 10f;
  17.  
  18.     public Orientation orientation = Orientation.forward;
  19.  
  20.     public bool isActive
  21.     { get; set; }
  22.  
  23.     private static CameraRotator _instance;
  24.     public static CameraRotator instance
  25.     {
  26.         get
  27.         {
  28.             if (_instance == null)
  29.             {
  30.                 _instance = GameObject.FindObjectOfType<CameraRotator>();
  31.                 DontDestroyOnLoad(_instance.gameObject);
  32.             }
  33.  
  34.             return _instance;
  35.         }
  36.     }
  37.  
  38.     void Awake()
  39.     {
  40.         if (_instance == null)
  41.         {
  42.             _instance = this;
  43.             DontDestroyOnLoad(this);
  44.         }
  45.         else
  46.         {
  47.             if (this != _instance)
  48.                 Destroy(this.gameObject);
  49.         }
  50.  
  51.         isActive = false;
  52.     }
  53.  
  54.  
  55.     public Vector3 oV()
  56.     {
  57.         switch (orientation)
  58.         {
  59.             case Orientation.up:
  60.                 return Vector3.up;
  61.             case Orientation.down:
  62.                 return Vector3.down;
  63.             case Orientation.left:
  64.                 return Vector3.left;
  65.             case Orientation.right:
  66.                 return Vector3.right;
  67.             case Orientation.forward:
  68.                 return Vector3.forward;
  69.             case Orientation.back:
  70.                 return Vector3.back;
  71.             default:
  72.                 return Vector3.forward;
  73.         }
  74.     }
  75.     float angle;
  76.     bool isDragging;
  77.     Vector3 mouseStart, mousePos, worldPos, vectorToTarget;
  78.     Quaternion q;
  79.     void Update()
  80.     {
  81.         if (gravityOn)
  82.             Physics.gravity = gravScale * gameObject.transform.up;
  83.  
  84.         if (!isActive)
  85.         {
  86.             mouseStart = Vector3.zero;
  87.             angle = 0f;
  88.             return;
  89.         }
  90.  
  91.         if (Input.GetMouseButtonDown(0))
  92.             mouseStart = Input.mousePosition;
  93.  
  94.         mousePos = Input.mousePosition;
  95.  
  96.  
  97.         if (Input.GetMouseButton(0) && (mousePos - mouseStart).sqrMagnitude > minSqrDist)
  98.         {
  99.             isDragging = true;
  100.            
  101.             mousePos.z = 100;
  102.             worldPos = Camera.main.ScreenToWorldPoint(mousePos);
  103.             vectorToTarget = worldPos - gameObject.transform.position;
  104.             angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
  105.             q = Quaternion.AngleAxis(angle, oV());
  106.             gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, q, Time.deltaTime * speed);
  107.            
  108.         }
  109.     }
  110.  
  111.     void OnMouseUp()
  112.     {
  113.         isDragging = false;
  114.     }
  115.  
  116.     void OnGUI()
  117.     {
  118.         if (!debugOn)
  119.             return;
  120.  
  121.         GUI.Label(new Rect(0, 0, 500, 500),
  122.             angle.ToString() + "\ndragging: " + isDragging.ToString()
  123.             + "\nmouse: " + mousePos.ToString()
  124.             + "\nworld: " + worldPos.ToString()
  125.             + "\nvtt: " + vectorToTarget.ToString()
  126.             + "\nq: " + q.ToString()
  127.             + "\ndown: " + gameObject.transform.up);
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement