|
@@ -0,0 +1,227 @@
|
|
|
+using System.Collections;
|
|
|
+using System.Collections.Generic;
|
|
|
+using UnityEngine;
|
|
|
+using UnityEngine.EventSystems;
|
|
|
+
|
|
|
+public class CameraController : MonoBehaviour
|
|
|
+{
|
|
|
+ public enum RotationAxes { MouseXAndY, MouseX, MouseY, RelativeObject, KeyBoard }
|
|
|
+ public RotationAxes axes = RotationAxes.MouseXAndY;
|
|
|
+ public float sensitivityX = 2F;
|
|
|
+ public float sensitivityY = 2F;
|
|
|
+ public float minimumX = -360F;
|
|
|
+ public float maximumX = 360F;
|
|
|
+ public float minimumY = -90F;
|
|
|
+ public float maximumY = 90F;
|
|
|
+ float rotationY = -60F;
|
|
|
+
|
|
|
+ // For camera movement
|
|
|
+ //float CameraPanningSpeed = 10.0f;
|
|
|
+
|
|
|
+ // Target Rotate
|
|
|
+ public Transform target;
|
|
|
+ public Vector3 offset;
|
|
|
+ public float sensitivity = 3; // чувствительность мышки
|
|
|
+ public float limit = 90; // ограничение вращения по Y
|
|
|
+ public float zoom = 0.5f; // чувствительность при увеличении, колесиком мышки
|
|
|
+ public float zoomMax = 50; // макс. увеличение
|
|
|
+ public float zoomMin = 0.5f; // мин. увеличение
|
|
|
+ private float X, Y;
|
|
|
+
|
|
|
+
|
|
|
+ void Start()
|
|
|
+ {
|
|
|
+ limit = Mathf.Abs(limit);
|
|
|
+ if (limit > 90) limit = 90;
|
|
|
+ offset = new Vector3(offset.x, offset.y, offset.z/*-Mathf.Abs(zoomMax) / 2*/);
|
|
|
+ transform.position = target.position + offset;
|
|
|
+ }
|
|
|
+
|
|
|
+ void Update()
|
|
|
+ {
|
|
|
+ if(axes == RotationAxes.KeyBoard)KeyboardsCommands();
|
|
|
+ else MouseInput();
|
|
|
+ }
|
|
|
+
|
|
|
+ void MouseInput()
|
|
|
+ {
|
|
|
+ if (EventSystem.current.IsPointerOverGameObject())
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Input.GetMouseButton(0))
|
|
|
+ {
|
|
|
+ }
|
|
|
+ else if (Input.GetMouseButton(1))
|
|
|
+ {
|
|
|
+ MouseRightClick();
|
|
|
+ }
|
|
|
+ else if (Input.GetMouseButton(2))
|
|
|
+ {
|
|
|
+ MouseMiddleButtonClicked();
|
|
|
+ }
|
|
|
+ else if (Input.GetMouseButtonUp(1))
|
|
|
+ {
|
|
|
+ ShowAndUnlockCursor();
|
|
|
+ }
|
|
|
+ else if (Input.GetMouseButtonUp(2))
|
|
|
+ {
|
|
|
+ ShowAndUnlockCursor();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ MouseWheeling();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void ShowAndUnlockCursor()
|
|
|
+ {
|
|
|
+ Cursor.lockState = CursorLockMode.None;
|
|
|
+ Cursor.visible = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ void HideAndLockCursor()
|
|
|
+ {
|
|
|
+ Cursor.lockState = CursorLockMode.Locked;
|
|
|
+ Cursor.visible = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ void MouseMiddleButtonClicked()
|
|
|
+ {
|
|
|
+ HideAndLockCursor();
|
|
|
+ Vector3 NewPosition = new Vector3(Input.GetAxis("Mouse X"), 0, Input.GetAxis("Mouse Y"));
|
|
|
+ Vector3 pos = transform.position;
|
|
|
+ if (NewPosition.x > 0.0f)
|
|
|
+ {
|
|
|
+ pos += transform.right;
|
|
|
+ }
|
|
|
+ else if (NewPosition.x < 0.0f)
|
|
|
+ {
|
|
|
+ pos -= transform.right;
|
|
|
+ }
|
|
|
+ if (NewPosition.z > 0.0f)
|
|
|
+ {
|
|
|
+ pos += transform.forward;
|
|
|
+ }
|
|
|
+ if (NewPosition.z < 0.0f)
|
|
|
+ {
|
|
|
+ pos -= transform.forward;
|
|
|
+ }
|
|
|
+ pos.y = transform.position.y;
|
|
|
+ transform.position = pos;
|
|
|
+ }
|
|
|
+
|
|
|
+ void MouseRightClick()
|
|
|
+ {
|
|
|
+ HideAndLockCursor();
|
|
|
+ switch (axes)
|
|
|
+ {
|
|
|
+ case RotationAxes.MouseXAndY:
|
|
|
+
|
|
|
+
|
|
|
+ float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
|
|
|
+
|
|
|
+ rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
|
|
|
+ rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
|
|
|
+
|
|
|
+ transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case RotationAxes.MouseX:
|
|
|
+
|
|
|
+ transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
|
|
|
+ break;
|
|
|
+ case RotationAxes.RelativeObject:
|
|
|
+
|
|
|
+ /*if (Input.GetAxis("Mouse ScrollWheel") > 0)
|
|
|
+ offset.z += zoom;
|
|
|
+ else if (Input.GetAxis("Mouse ScrollWheel") < 0)
|
|
|
+ offset.z -= zoom;*/
|
|
|
+ //offset.z = Mathf.Clamp(offset.z, -Mathf.Abs(zoomMax), -Mathf.Abs(zoomMin));
|
|
|
+
|
|
|
+ X = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivity;
|
|
|
+ Y += Input.GetAxis("Mouse Y") * sensitivity;
|
|
|
+ Y = Mathf.Clamp(Y, -limit, limit);
|
|
|
+ transform.localEulerAngles = new Vector3(-Y, X, 0);
|
|
|
+ transform.position = transform.localRotation * offset + target.position;
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
|
|
|
+ rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
|
|
|
+
|
|
|
+ transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void MouseWheeling()
|
|
|
+ {
|
|
|
+ /*Vector3 pos = transform.position;
|
|
|
+ if (Input.GetAxis("Mouse ScrollWheel") < 0)
|
|
|
+ {
|
|
|
+ pos = pos - transform.forward;
|
|
|
+ transform.position = pos;
|
|
|
+ }
|
|
|
+ if (Input.GetAxis("Mouse ScrollWheel") > 0)
|
|
|
+ {
|
|
|
+ pos = pos + transform.forward;
|
|
|
+ transform.position = pos;
|
|
|
+ }*/
|
|
|
+
|
|
|
+ if (Input.GetAxis("Mouse ScrollWheel") > 0)
|
|
|
+ {
|
|
|
+ offset.z += zoom;
|
|
|
+ }
|
|
|
+ else if (Input.GetAxis("Mouse ScrollWheel") < 0)
|
|
|
+ offset.z -= zoom;
|
|
|
+ offset.z = Mathf.Clamp(offset.z, -Mathf.Abs(zoomMax), -Mathf.Abs(zoomMin));
|
|
|
+
|
|
|
+ transform.position = transform.localRotation * offset + target.position;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Vector3 GetBaseInput()
|
|
|
+ { //returns the basic values, if it's 0 than it's not active.
|
|
|
+ Vector3 p_Velocity = new Vector3();
|
|
|
+ if (Input.GetKey(KeyCode.W))
|
|
|
+ {
|
|
|
+ p_Velocity += new Vector3(0, 0, 1);
|
|
|
+ }
|
|
|
+ if (Input.GetKey(KeyCode.S))
|
|
|
+ {
|
|
|
+ p_Velocity += new Vector3(0, 0, -1);
|
|
|
+ }
|
|
|
+ if (Input.GetKey(KeyCode.A))
|
|
|
+ {
|
|
|
+ p_Velocity += new Vector3(-1, 0, 0);
|
|
|
+ }
|
|
|
+ if (Input.GetKey(KeyCode.D))
|
|
|
+ {
|
|
|
+ p_Velocity += new Vector3(1, 0, 0);
|
|
|
+ }
|
|
|
+ return p_Velocity;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void KeyboardsCommands()
|
|
|
+ {
|
|
|
+ //Keyboard commands
|
|
|
+ //float f = 0.0f;
|
|
|
+ Vector3 p = GetBaseInput();
|
|
|
+
|
|
|
+
|
|
|
+ p = p * Time.deltaTime;
|
|
|
+ Vector3 newPosition = transform.position;
|
|
|
+ if (Input.GetKey(KeyCode.Space))
|
|
|
+ { //If player wants to move on X and Z axis only
|
|
|
+ transform.Translate(p);
|
|
|
+ newPosition.x = transform.position.x;
|
|
|
+ newPosition.z = transform.position.z;
|
|
|
+ transform.position = newPosition;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ transform.Translate(p);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|