add new input mouse system
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using Cinemachine;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class CameraSystem : MonoBehaviour
|
||||
{
|
||||
@@ -41,7 +43,6 @@ public class CameraSystem : MonoBehaviour
|
||||
private float _zoomSpeed = 2f;
|
||||
[SerializeField]
|
||||
private float _zoomAmount = 3f;
|
||||
private Vector2 _lastMousePosition;
|
||||
private float _targetFieldOfView = 60f;
|
||||
private CinemachineTransposer _cinemachineTransposer;
|
||||
|
||||
@@ -56,14 +57,36 @@ public class CameraSystem : MonoBehaviour
|
||||
transform.position = Player.Instance.transform.position;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
InputManager.Instance.CameraAction.MouseRotate.performed += OnCameraRotate;
|
||||
InputManager.Instance.CameraAction.Zoom.performed += OnCameraZoom;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
InputManager.Instance.CameraAction.MouseRotate.performed -= OnCameraRotate;
|
||||
InputManager.Instance.CameraAction.Zoom.performed -= OnCameraZoom;
|
||||
}
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
HandleCameraMovement();
|
||||
HandleCameraRotation();
|
||||
var cameraMove = InputManager.Instance.CameraAction.Move.ReadValue<Vector2>();
|
||||
|
||||
if (cameraMove.sqrMagnitude > 0.1f)
|
||||
{
|
||||
HandleCameraMovement(cameraMove);
|
||||
}
|
||||
|
||||
float rotate = InputManager.Instance.CameraAction.Rotate.ReadValue<float>();
|
||||
if (rotate != 0)
|
||||
{
|
||||
HandleCameraRotation(rotate, _rotateSpeed);
|
||||
}
|
||||
//HandleCameraZoom_FOV();
|
||||
//HandleCameraZoom_MoveForward();
|
||||
HandleCameraZoom_MoveY();
|
||||
//HandleCameraZoom_MoveY();
|
||||
}
|
||||
|
||||
private void HandleCameraZoom_MoveForward()
|
||||
@@ -85,12 +108,11 @@ public class CameraSystem : MonoBehaviour
|
||||
_cinemachineTransposer.m_FollowOffset = _followOffset;
|
||||
}
|
||||
|
||||
private void HandleCameraZoom_MoveY()
|
||||
private void OnCameraZoom(InputAction.CallbackContext context)
|
||||
{
|
||||
if (Input.mouseScrollDelta.y > 0)
|
||||
_followOffset.y -= _zoomAmount;
|
||||
if (Input.mouseScrollDelta.y < 0)
|
||||
_followOffset.y += _zoomAmount;
|
||||
float _zoomAmount = InputManager.Instance.CameraAction.Rotate.ReadValue<float>();
|
||||
|
||||
_followOffset.y += _zoomAmount;
|
||||
|
||||
_followOffset.y = Mathf.Clamp(_followOffset.y, _followOffsetMinY, _followOffsetMaxY);
|
||||
|
||||
@@ -100,8 +122,6 @@ public class CameraSystem : MonoBehaviour
|
||||
if (_followOffset.magnitude > _followOffsetMax)
|
||||
_followOffset.y = _zoomAmount * _followOffsetMax;
|
||||
|
||||
|
||||
|
||||
Vector3.Lerp(_cinemachineTransposer.m_FollowOffset, _followOffset, Time.deltaTime * _zoomSpeed);
|
||||
|
||||
_cinemachineTransposer.m_FollowOffset = _followOffset;
|
||||
@@ -119,15 +139,8 @@ public class CameraSystem : MonoBehaviour
|
||||
_camera.m_Lens.FieldOfView = _targetFieldOfView;
|
||||
}
|
||||
|
||||
private void HandleCameraMovement()
|
||||
private void HandleCameraMovement(Vector2 inputDir)
|
||||
{
|
||||
var inputDir = Vector3.zero;
|
||||
|
||||
if (Input.GetKey(KeyCode.W)) inputDir.z = 1f;
|
||||
if (Input.GetKey(KeyCode.S)) inputDir.z = -1f;
|
||||
if (Input.GetKey(KeyCode.A)) inputDir.x = -1f;
|
||||
if (Input.GetKey(KeyCode.D)) inputDir.x = 1f;
|
||||
|
||||
if (_useEdgeScrolling)
|
||||
{
|
||||
inputDir = EdgeScrollingMovement(inputDir);
|
||||
@@ -137,27 +150,27 @@ public class CameraSystem : MonoBehaviour
|
||||
inputDir = MousePanMovement(inputDir);
|
||||
}
|
||||
|
||||
Vector3 moveDir = transform.forward * inputDir.z + transform.right * inputDir.x;
|
||||
Vector3 moveDir = transform.forward * inputDir.y + transform.right * inputDir.x;
|
||||
transform.position += moveDir * _moveSpeed * Time.deltaTime;
|
||||
}
|
||||
|
||||
private Vector3 MousePanMovement(Vector3 inputDir)
|
||||
{
|
||||
if (Input.GetMouseButtonDown(1))
|
||||
{
|
||||
_dragPanMoveActive = true;
|
||||
_lastMousePosition = Input.mousePosition;
|
||||
}
|
||||
if (Input.GetMouseButtonUp(1)) { _dragPanMoveActive = false; }
|
||||
// if (Input.GetMouseButtonDown(1))
|
||||
// {
|
||||
// _dragPanMoveActive = true;
|
||||
// _lastMousePosition = Input.mousePosition;
|
||||
// }
|
||||
// if (Input.GetMouseButtonUp(1)) { _dragPanMoveActive = false; }
|
||||
|
||||
if (_dragPanMoveActive)
|
||||
{
|
||||
Vector2 mouseMovementDelta = ((Vector2)Input.mousePosition - _lastMousePosition) * _dragPanSpeed;
|
||||
inputDir.x = mouseMovementDelta.x;
|
||||
inputDir.z = mouseMovementDelta.y;
|
||||
// if (_dragPanMoveActive)
|
||||
// {
|
||||
// Vector2 mouseMovementDelta = ((Vector2)Input.mousePosition - _lastMousePosition) * _dragPanSpeed;
|
||||
// inputDir.x = mouseMovementDelta.x;
|
||||
// inputDir.z = mouseMovementDelta.y;
|
||||
|
||||
_lastMousePosition = Input.mousePosition;
|
||||
}
|
||||
// _lastMousePosition = Input.mousePosition;
|
||||
// }
|
||||
|
||||
return inputDir;
|
||||
}
|
||||
@@ -171,29 +184,17 @@ public class CameraSystem : MonoBehaviour
|
||||
return inputDir;
|
||||
}
|
||||
|
||||
private void HandleCameraRotation()
|
||||
private void OnCameraRotate(InputAction.CallbackContext context)
|
||||
{
|
||||
float rotateDir = 0f;
|
||||
|
||||
if (Input.GetKey(KeyCode.E)) rotateDir = 1f;
|
||||
if (Input.GetKey(KeyCode.Q)) rotateDir = -1f;
|
||||
if (_useMouseRotate)
|
||||
if (!_useMouseRotate && !Mouse.current.middleButton.isPressed)
|
||||
{
|
||||
if (Input.GetMouseButtonDown(2))
|
||||
{
|
||||
_dragRotateMoveActive = true;
|
||||
_lastMousePosition = Input.mousePosition;
|
||||
}
|
||||
if (Input.GetMouseButtonUp(2)) { _dragRotateMoveActive = false; }
|
||||
if (_dragRotateMoveActive)
|
||||
{
|
||||
Vector2 mouseMovementDelta = ((Vector2)Input.mousePosition - _lastMousePosition);
|
||||
rotateDir = mouseMovementDelta.x;
|
||||
|
||||
_lastMousePosition = Input.mousePosition;
|
||||
}
|
||||
return;
|
||||
}
|
||||
var speed = _dragRotateMoveActive ? _mouseRotationSpeed : _rotateSpeed;
|
||||
HandleCameraRotation(Mouse.current.delta.x.value, _mouseRotationSpeed);
|
||||
}
|
||||
|
||||
private void HandleCameraRotation(float rotateDir, float speed)
|
||||
{
|
||||
transform.eulerAngles += new Vector3(0, rotateDir * speed * Time.deltaTime, 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user