123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- /// <summary>
- /// Отслеживание объекта
- /// </summary>
- 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);
- }
- }
- }
|