123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- 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, Mouse, TrackMarker }
- 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;
- if (axes != RotationAxes.Mouse) offset = new Vector3(offset.x, offset.y, offset.z/*-Mathf.Abs(zoomMax) / 2*/);
- else
- offset = transform.position;
- if(target != null) transform.position = target.position + offset;
- targetPosition = transform.position;
- }
- void Update()
- {
- switch (axes)
- {
- case RotationAxes.KeyBoard:
- KeyboardsCommands();
- break;
- case RotationAxes.Mouse:
- MouseCommands();
- break;
- default:
- MouseInput();
- break;
- }
- }
- 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);
- }
- }
- public float movementSpeed = 0.1f;
- public float rotationSpeed = 4f;
- public float smoothness = 0.85f;
- Vector3 targetPosition;
- public Quaternion targetRotation;
- float targetRotationY;
- float targetRotationX;
- public float dragSpeed = 0.00001f;
- private Vector3 dragOrigin;
- private void MouseCommands()
- {
- if (Input.GetKey(KeyCode.W))
- targetPosition += transform.forward * movementSpeed;
- if (Input.GetKey(KeyCode.A))
- targetPosition -= transform.right * movementSpeed;
- if (Input.GetKey(KeyCode.S))
- targetPosition -= transform.forward * movementSpeed;
- if (Input.GetKey(KeyCode.D))
- targetPosition += transform.right * movementSpeed;
- if (Input.GetKey(KeyCode.Q))
- targetPosition -= transform.up * movementSpeed;
- if (Input.GetKey(KeyCode.E))
- targetPosition += transform.up * movementSpeed;
- //
- if (Input.GetMouseButtonDown(2))
- {
- dragOrigin = Input.mousePosition;
- //return;
- }
- //if (!Input.GetMouseButton(2)) return;
- if (Input.GetMouseButton(1))
- {
- Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
- if(pos.x < 0)
- targetPosition -= transform.right * movementSpeed * 0.1f;
- if (pos.x>0)
- targetPosition += transform.right * movementSpeed * 0.1f;
- if (pos.y > 0)
- targetPosition -= transform.up * movementSpeed*0.1f;
- if (pos.y < 0)
- targetPosition += transform.up * movementSpeed * 0.1f;
- }
- if (Input.GetAxis("Mouse ScrollWheel") > 0)
- {
- //offset.z += zoom;
- targetPosition += transform.forward * movementSpeed;
- }
- else if (Input.GetAxis("Mouse ScrollWheel") < 0)
- {
- //offset.z -= zoom;
- targetPosition -= transform.forward * movementSpeed;
- }
- //offset.z = Mathf.Clamp(offset.z, -Mathf.Abs(zoomMax), -Mathf.Abs(zoomMin));
- //if (Input.GetAxis("Mouse ScrollWheel") != 0)
- //{
- // 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 = new Vector3(transform.position.x, Y, transform.position.z);
-
- //}
-
- //transform.position = transform.localRotation * offset;
- //if (Input.GetMouseButton(1))
- //{
- // Cursor.visible = false;
- // targetRotationY += Input.GetAxis("Mouse X") * rotationSpeed;
- // targetRotationX -= Input.GetAxis("Mouse Y") * rotationSpeed;
- // targetRotation = Quaternion.Euler(targetRotationX, targetRotationY, 0.0f);
- //}
- //else
- // Cursor.visible = true;
- transform.position = Vector3.Lerp(transform.position, targetPosition, (1.0f - smoothness));
- //transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, (1.0f - smoothness));
- }
- }
|