Last active
April 30, 2019 01:35
-
-
Save grayrhino/3f3c8d6cc6a08ae45aee0da627f9aa51 to your computer and use it in GitHub Desktop.
I have been working on a Camera Controller for Unity that can be used for large area scenes. The code is not entirely mine but I believe it is worth sharing since I couldn't find something similar that easily.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.EventSystems; | |
/* | |
* In order for the code below to work without error you have to: | |
* 1. Add the script to the Main Camera | |
* 2. In Inspector change the Main Camera tag to MainCamera | |
* 3. In Hierarchy create UI element EventSystem | |
*/ | |
public class CameraController : MonoBehaviour | |
{ | |
//Pan | |
private float sensitivityX; | |
private float sensitivityY; | |
public Vector3 MousePosV3; | |
//Zoom | |
public int distanceMin = -100; | |
public int distanceMax = 500; | |
public float zoomSpeed = 50f; | |
private Vector3 moveDirection = Vector3.zero; | |
// Orbit | |
public float minY = -45.0f; | |
public float maxY = 45.0f; | |
public float OrbitsensX = 100.0f; | |
public float OrbitsensY = 100.0f; | |
float rotationY = 0.0f; | |
float rotationX = 0.0f; | |
// Check Mouse on UI Layer | |
static public bool checkMouseOnUI; | |
private Vector3 currentMainCameraPos; | |
private Quaternion currentMainCameraRot; | |
public bool isOnable = true; | |
readonly bool isPan = true; | |
readonly bool isOrbit = true; | |
readonly bool isZoom = true; | |
public bool rockTarget = false; | |
#region | |
public virtual void IsEnable(bool b) | |
{ | |
isOnable = b; | |
} | |
#endregion | |
private void Start() | |
{ | |
rotationY = -transform.localEulerAngles.x; | |
rotationX = transform.localEulerAngles.y; | |
currentMainCameraPos = transform.position; | |
currentMainCameraRot = Quaternion.Euler(transform.eulerAngles); | |
} | |
private void OnEnable() | |
{ | |
RaycastHit hits; | |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
if (Physics.Raycast(ray, out hits, 10000)) | |
{ | |
MousePosV3 = new Vector3(hits.point.x, hits.point.y, hits.point.z); | |
} | |
} | |
private void Update() | |
{ | |
if (!isOnable) | |
{ | |
if (Input.GetMouseButton(0)) | |
{ | |
isOnable = true; | |
} | |
return; | |
} | |
if (isOnable) | |
{ | |
if (checkMouseOnUI == false) | |
{ | |
if (EventSystem.current.IsPointerOverGameObject()) | |
{ | |
return; | |
} | |
// Pan Start | |
if (isPan) | |
{ | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
RaycastHit hits; | |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
if (!rockTarget) | |
{ | |
if (Physics.Raycast(ray, out hits, 10000)) | |
{ | |
MousePosV3 = new Vector3(hits.point.x, hits.point.y, hits.point.z); | |
} | |
} | |
float distance = Vector3.Distance(MousePosV3, transform.position); | |
sensitivityX = distance * 0.02f; | |
sensitivityY = distance * 0.02f; | |
} | |
float deltaX = Input.GetAxis("Mouse X") * sensitivityX; | |
float deltaY = Input.GetAxis("Mouse Y") * sensitivityY; | |
if (Input.GetMouseButton(0)) | |
{ | |
transform.position += deltaX * transform.right * -1; | |
transform.position += deltaY * transform.up * -1; | |
} | |
} | |
// Pan end | |
//Zoom Start | |
if (isZoom) | |
{ | |
moveDirection = new Vector3(0, 0, Input.GetAxis("Mouse ScrollWheel") * zoomSpeed); | |
if (!Input.GetKey(KeyCode.LeftShift)) | |
{ | |
if (Input.GetAxis("Mouse ScrollWheel") > 0) | |
{ | |
if (Mathf.Floor(transform.position.y + moveDirection.y) > distanceMin) | |
{ | |
transform.Translate(moveDirection, Space.Self); | |
} | |
} | |
else if (Input.GetAxis("Mouse ScrollWheel") < 0) | |
{ | |
// We shouldn't be allowed to zoom out more than distanceMax | |
if (Mathf.Floor(transform.position.y + moveDirection.y) < distanceMax) | |
{ | |
transform.Translate(moveDirection, Space.Self); | |
} | |
} | |
} | |
} | |
// Zoom End | |
// Orbit Start | |
if (isOrbit) | |
{ | |
if (Input.GetMouseButton(2)) | |
{ | |
transform.RotateAround(MousePosV3, Vector3.up, Input.GetAxis("Mouse X") * 2); | |
transform.RotateAround(MousePosV3, transform.right * -1, Input.GetAxis("Mouse Y") * 2); | |
} | |
} | |
//Orbit End | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment