Implemented simple computer camera controls.
This commit is contained in:
parent
e7ea8302ad
commit
f4c7a5d784
2 changed files with 43 additions and 0 deletions
41
PuzzleGameProject/Assets/Scripts/PlayerController.cs
Normal file
41
PuzzleGameProject/Assets/Scripts/PlayerController.cs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
private Vector3 _lastMousePosition;
|
||||
private Camera _camera;
|
||||
private bool _canMove;
|
||||
|
||||
private void Start() {
|
||||
_camera = Camera.main;
|
||||
}
|
||||
|
||||
private void Update() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue