using System.Collections.Generic; using System.Runtime.CompilerServices; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.InputSystem; using UnityEngine.InputSystem.Controls; public class MoveCamera : Singleton { /// /// 카메라 시점 변경 속도 /// public float CameraRotationSpeed = 4; /// /// 마우스 휠 줌 속도 /// public float ZoomSpeed = 50.0f; public float ZoomMoveSpeed = 5f; /// /// 줌 최솟값, 작을 수록 확대할 수 있는 수치가 커짐 /// public float MinZoomValue = 10; /// /// 줌 최대값, 클 수록 축소할 수 있는 수치가 커짐 /// public float MaxZoomValue = 90; /// /// 카메라 이동 속도 /// [Range(1f, 10f)] public float CameraMoveSpeed = 4; /// /// 마우스를 움직였을때의 x,y 좌표값 /// [SerializeField] float mouseY = 0; [SerializeField] float mouseX = 0; //bool mouseOverCanvas = false; /// /// 마우스 왼쪽버튼 클릭에 대한 컨트롤 /// private ButtonControl mouseLeftButton; //[SerializeField] //private float m_CanMoveDistance = 3f; [SerializeField] /// /// 포인트 이동 시 초기 위치 값 저장 /// private Vector3 m_CameraInitOffset; private Camera m_Camera; private float initFieldOfView = 60.0f; private float targetFieldOfView = 60.0f; public override void Awake() { } void Start() { m_Camera = Camera.main; mouseLeftButton = Mouse.current.leftButton; MouseInitialize(transform); } void Update() { CameraInputMove(); if (mouseLeftButton.isPressed && !IsPointerOverCanvas()) { Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto); mouseY += Input.GetAxis("Mouse Y") * CameraRotationSpeed; mouseY = Mathf.Clamp(mouseY, -90, 90); mouseX += Input.GetAxis("Mouse X") * CameraRotationSpeed; transform.localEulerAngles = new Vector3(-mouseY, mouseX, 0); } float scrollValue = Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed; if(scrollValue < 0 && m_Camera.fieldOfView > MinZoomValue) { targetFieldOfView = m_Camera.fieldOfView + scrollValue; } else if(scrollValue > 0 && m_Camera.fieldOfView < MaxZoomValue) { targetFieldOfView = m_Camera.fieldOfView + scrollValue; } if(Input.GetKeyUp(KeyCode.Mouse2)) { targetFieldOfView = initFieldOfView; } m_Camera.fieldOfView = Mathf.Lerp(m_Camera.fieldOfView, targetFieldOfView , Time.deltaTime * ZoomMoveSpeed); } bool IsPointerOverCanvas() { if (EventSystem.current == null) return false; PointerEventData eventData = new PointerEventData(EventSystem.current); eventData.position = Input.mousePosition; var results = new List(); EventSystem.current.RaycastAll(eventData, results); return results.Count > 0; } public void SetCameraInitOffset(Vector3 pos) { m_CameraInitOffset = pos; } float getMouseY(float x) { if (x > 90) { if (x > 270) { //360도 까지만 있다는 전제하에 return (x - 360); } return (180 - (x)); } else if (x < -90) { if (x < -270) { return (x + 360); } return (180 + (x)); } else { return x; } } public void MouseInitialize(Transform CameraTransform) { mouseX = CameraTransform.localEulerAngles.y; mouseY = -getMouseY(CameraTransform.localEulerAngles.x); } public void MouseInitialize() { mouseX = 0; mouseY = 0; } private void CameraInputMove() { Vector3 directionMovePos(Vector3 direction) { return direction * CameraMoveSpeed * Time.deltaTime; } Vector3 movePosition = Vector3.zero; if (Input.GetKey(InputManager.Forward)) movePosition += directionMovePos(transform.forward); if (Input.GetKey(InputManager.Backword)) movePosition -= directionMovePos(transform.forward); if (Input.GetKey(InputManager.Right)) movePosition += directionMovePos(transform.right); if (Input.GetKey(InputManager.Left)) movePosition -= directionMovePos(transform.right); if (Input.GetKey(InputManager.Up)) movePosition += directionMovePos(transform.up); if (Input.GetKey(InputManager.Down)) movePosition -= directionMovePos(transform.up); transform.position += movePosition; if(transform.position.y < 0) { movePosition =new Vector3(transform.position.x, 0, transform.position.z); transform.position = movePosition; } } }