Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CameraCtrl : MonoBehaviour {
- public Camera[] SyncedCameras;
- public float Radius = 5;
- public float theta = 0;
- public float phi = 0;
- private const float angleSpeed = 0.005f;
- private const float radiusSpeed = 0.1f;
- private const float radiusFar = 100;
- private const float radiusNear = 0.1f;
- private const float phiLimit = Mathf.PI * 0.45f;
- // Use this for initialization
- void Start () {
- mouseLeftPressed = false;
- mouseRightPressed = false;
- UpdateCameras();
- }
- // Update is called once per frame
- void Update ()
- {
- bool cameraDirty = false;
- if ((Input.GetMouseButton(0) == true) || (Input.GetMouseButton(1) == true))
- {
- //Mouse left - rotation
- if (Input.GetMouseButton(0) == true)
- {
- if (mouseLeftPressed == false)
- {
- mouseLeftPressed = true;
- }
- else
- {
- float mouseDeltaX = Input.mousePosition.x - mouseLastX;
- float mouseDeltaY = Input.mousePosition.y - mouseLastY;
- theta += mouseDeltaX * angleSpeed;
- if (theta > Mathf.PI) theta -= 2 * Mathf.PI;
- if (theta < -Mathf.PI) theta += 2 * Mathf.PI;
- phi += - mouseDeltaY * angleSpeed;
- if (phi > phiLimit) phi = phiLimit;
- if (phi < -phiLimit) phi = -phiLimit;
- cameraDirty = true;
- }
- } else
- {
- mouseLeftPressed = false;
- }
- //Mouse right button -Zooming (radius)
- if (Input.GetMouseButton(1) == true)
- {
- if (mouseRightPressed == false)
- {
- mouseRightPressed = true;
- }
- else
- {
- float mouseDeltaY = Input.mousePosition.y - mouseLastY;
- Radius += - mouseDeltaY * radiusSpeed;
- if (Radius < radiusNear) Radius = radiusNear;
- if (Radius > radiusFar) Radius = - radiusFar;
- cameraDirty = true;
- }
- }
- }
- if (cameraDirty) UpdateCameras();
- mouseLastX = Input.mousePosition.x;
- mouseLastY = Input.mousePosition.y;
- }
- void UpdateCameras()
- {
- Vector3 CameraPos = new Vector3(Mathf.Sin(theta),
- Mathf.Tan(phi),
- Mathf.Cos(theta));
- CameraPos.Normalize();
- CameraPos *= Radius;
- foreach (Camera cam in SyncedCameras)
- {
- cam.transform.position = CameraPos;
- cam.transform.LookAt(Vector3.zero);
- }
- }
- bool mouseLeftPressed, mouseRightPressed;
- float mouseLastX, mouseLastY;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement