56 lines
1.4 KiB
C#
56 lines
1.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
private Vector3 _lastMousePosition;
|
|
private Camera _camera;
|
|
private bool _canMove;
|
|
private int _zoomScale = 2;
|
|
|
|
private void Start() {
|
|
_camera = Camera.main;
|
|
}
|
|
|
|
private void Update() {
|
|
switch (Input.GetAxis("Mouse ScrollWheel"))
|
|
{
|
|
case < 0:
|
|
_camera.orthographicSize += .1f * _zoomScale;
|
|
break;
|
|
case > 0:
|
|
if (_camera.orthographicSize > .1f * _zoomScale)
|
|
{
|
|
_camera.orthographicSize -= .1f * _zoomScale;
|
|
|
|
}
|
|
break;
|
|
}
|
|
|
|
CheckMovementInput();
|
|
}
|
|
|
|
private void CheckMovementInput() {
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
_lastMousePosition = Input.mousePosition;
|
|
_canMove = !IsMouseOverUI();
|
|
}
|
|
|
|
if (!Input.GetMouseButton(0) || !_canMove) return;
|
|
|
|
Vector3 mouseWorldPoint = _camera.ScreenToWorldPoint(Input.mousePosition);
|
|
Vector3 lastWorldPoint = _camera.ScreenToWorldPoint(_lastMousePosition);
|
|
|
|
Vector3 delta = mouseWorldPoint - lastWorldPoint;
|
|
|
|
_lastMousePosition = Input.mousePosition;
|
|
|
|
_camera.transform.position -= delta;
|
|
}
|
|
|
|
private bool IsMouseOverUI() {
|
|
return EventSystem.current.IsPointerOverGameObject();
|
|
}
|
|
}
|
|
|