Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public enum Orientation
- {
- up,down,left,right,forward,back
- }
- public class CameraRotator : MonoBehaviour
- {
- public float speed = 90f;
- public bool gravityOn = true;
- public float gravScale = -100f;
- public bool debugOn = false;
- public float minSqrDist = 10f;
- public Orientation orientation = Orientation.forward;
- public bool isActive
- { get; set; }
- private static CameraRotator _instance;
- public static CameraRotator instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = GameObject.FindObjectOfType<CameraRotator>();
- DontDestroyOnLoad(_instance.gameObject);
- }
- return _instance;
- }
- }
- void Awake()
- {
- if (_instance == null)
- {
- _instance = this;
- DontDestroyOnLoad(this);
- }
- else
- {
- if (this != _instance)
- Destroy(this.gameObject);
- }
- isActive = false;
- }
- public Vector3 oV()
- {
- switch (orientation)
- {
- case Orientation.up:
- return Vector3.up;
- case Orientation.down:
- return Vector3.down;
- case Orientation.left:
- return Vector3.left;
- case Orientation.right:
- return Vector3.right;
- case Orientation.forward:
- return Vector3.forward;
- case Orientation.back:
- return Vector3.back;
- default:
- return Vector3.forward;
- }
- }
- float angle;
- bool isDragging;
- Vector3 mouseStart, mousePos, worldPos, vectorToTarget;
- Quaternion q;
- void Update()
- {
- if (gravityOn)
- Physics.gravity = gravScale * gameObject.transform.up;
- if (!isActive)
- {
- mouseStart = Vector3.zero;
- angle = 0f;
- return;
- }
- if (Input.GetMouseButtonDown(0))
- mouseStart = Input.mousePosition;
- mousePos = Input.mousePosition;
- if (Input.GetMouseButton(0) && (mousePos - mouseStart).sqrMagnitude > minSqrDist)
- {
- isDragging = true;
- mousePos.z = 100;
- worldPos = Camera.main.ScreenToWorldPoint(mousePos);
- vectorToTarget = worldPos - gameObject.transform.position;
- angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
- q = Quaternion.AngleAxis(angle, oV());
- gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, q, Time.deltaTime * speed);
- }
- }
- void OnMouseUp()
- {
- isDragging = false;
- }
- void OnGUI()
- {
- if (!debugOn)
- return;
- GUI.Label(new Rect(0, 0, 500, 500),
- angle.ToString() + "\ndragging: " + isDragging.ToString()
- + "\nmouse: " + mousePos.ToString()
- + "\nworld: " + worldPos.ToString()
- + "\nvtt: " + vectorToTarget.ToString()
- + "\nq: " + q.ToString()
- + "\ndown: " + gameObject.transform.up);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement