Advertisement
napland

Cam rotator - no jump

May 7th, 2015
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 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.     {
  22.         get;
  23.         set;
  24.     }
  25.  
  26.     private static CameraRotator _instance;
  27.     public static CameraRotator instance
  28.     {
  29.         get
  30.         {
  31.             if (_instance == null)
  32.             {
  33.                 _instance = GameObject.FindObjectOfType<CameraRotator>();
  34.                 DontDestroyOnLoad(_instance.gameObject);
  35.             }
  36.  
  37.             return _instance;
  38.         }
  39.     }
  40.  
  41.     void Awake()
  42.     {
  43.         if (_instance == null)
  44.         {
  45.             _instance = this;
  46.             DontDestroyOnLoad(this);
  47.         }
  48.         else
  49.         {
  50.             if (this != _instance)
  51.                 Destroy(this.gameObject);
  52.         }
  53.  
  54.         isActive = false;
  55.     }
  56.  
  57.  
  58.     public Vector3 oV()
  59.     {
  60.         switch (orientation)
  61.         {
  62.             case Orientation.up:
  63.                 return Vector3.up;
  64.             case Orientation.down:
  65.                 return Vector3.down;
  66.             case Orientation.left:
  67.                 return Vector3.left;
  68.             case Orientation.right:
  69.                 return Vector3.right;
  70.             case Orientation.forward:
  71.                 return Vector3.forward;
  72.             case Orientation.back:
  73.                 return Vector3.back;
  74.             default:
  75.                 return Vector3.forward;
  76.         }
  77.     }
  78.     float angle;
  79.     bool isDragging;
  80.     Vector3 mouseStart, mousePos, worldPos, vectorToTarget;
  81.     Quaternion q;
  82.  
  83.     string debugLabel = "";
  84.     void Update()
  85.     {
  86.         if (gravityOn)
  87.             Physics.gravity = gravScale * gameObject.transform.up;
  88.  
  89.         if (!isActive)
  90.         {
  91.             return;
  92.         }
  93.  
  94.  
  95.  
  96.         if (Input.GetMouseButtonDown(0))
  97.         {
  98.             mouseStart = MousePosition();
  99.         }
  100.         else if (Input.GetMouseButton(0))
  101.         {
  102.             mousePos = MousePosition();
  103.             Vector3 dPos = mousePos - mouseStart;
  104.             float rotate = 0f;
  105.  
  106.             if (mousePos.y > 0.5f)
  107.                 rotate -= dPos.x;
  108.             else if (mousePos.y < 0.5f)
  109.                 rotate += dPos.x;
  110.  
  111.             if (mousePos.x > 0.5f)
  112.                 rotate += dPos.y;
  113.             else if (mousePos.x < 0.5f)
  114.                 rotate -= dPos.y;
  115.  
  116.             gameObject.transform.Rotate(Vector3.back, rotate * speed * Time.deltaTime);
  117.  
  118.             mouseStart = mousePos;
  119.         }
  120.  
  121.  
  122.         /*
  123.         if (Input.GetMouseButtonDown(0))
  124.         {
  125.             mouseStart = Input.mousePosition;
  126.             mouseStart.z = 100f;            
  127.         }
  128.         else if (Input.GetMouseButton(0))
  129.         {
  130.             isDragging = true;
  131.             mousePos = Input.mousePosition;
  132.             mousePos.z = 100f;
  133.             worldPos = Camera.main.ScreenToWorldPoint(mousePos);
  134.            
  135.             vectorToTarget = worldPos - gameObject.transform.position;
  136.             angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
  137.  
  138.             q = Quaternion.AngleAxis(angle, oV());
  139.             gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, q, Time.deltaTime * speed);
  140.  
  141.         }
  142.          */
  143.     }
  144.  
  145.  
  146.     Vector3 MousePosition()
  147.     {
  148.         return Camera.main.ScreenToViewportPoint(new Vector3 (Input.mousePosition.x , Input.mousePosition.y , -100f));
  149.         //return Input.mousePosition;
  150.     }
  151.  
  152.     void OnMouseUp()
  153.     {
  154.         isDragging = false;
  155.     }
  156.  
  157.     void OnGUI()
  158.     {
  159.         GUI.Label(new Rect(0, 0, 500, 500), debugLabel);
  160.  
  161.         if (!debugOn)
  162.             return;
  163.  
  164.         GUI.Label(new Rect(0, 0, 500, 500),
  165.             angle.ToString() + "\ndragging: " + isDragging.ToString()
  166.             + "\nmouse: " + mousePos.ToString()
  167.             + "\nworld: " + worldPos.ToString()
  168.             + "\nvtt: " + vectorToTarget.ToString()
  169.             + "\nq: " + q.ToString()
  170.             + "\ndown: " + gameObject.transform.up);
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement