mirror of
https://github.com/Xevion/Rebirth.git
synced 2025-12-06 23:16:00 -06:00
Camera panning logic
This commit is contained in:
@@ -21,6 +21,8 @@ public class CameraControl : MonoBehaviour
|
|||||||
public float TargetZoom { get { return _targetZoom; } }
|
public float TargetZoom { get { return _targetZoom; } }
|
||||||
private float _targetZoom;
|
private float _targetZoom;
|
||||||
private Vector2 _targetPosition;
|
private Vector2 _targetPosition;
|
||||||
|
private Vector2? _panningClickPosition;
|
||||||
|
private Vector3? _panningCameraPosition;
|
||||||
|
|
||||||
private new Camera camera;
|
private new Camera camera;
|
||||||
|
|
||||||
@@ -33,6 +35,18 @@ public class CameraControl : MonoBehaviour
|
|||||||
|
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
|
var isPanning = Input.GetMouseButton(1);
|
||||||
|
|
||||||
|
if (!isPanning)
|
||||||
|
{
|
||||||
|
if (_panningClickPosition != null)
|
||||||
|
{
|
||||||
|
_panningClickPosition = null;
|
||||||
|
_panningCameraPosition = null;
|
||||||
|
_targetPosition = (Vector2)transform.position;
|
||||||
|
_targetZoom = camera.orthographicSize;
|
||||||
|
}
|
||||||
|
|
||||||
// calculate target zoom
|
// calculate target zoom
|
||||||
var scroll = Input.GetAxisRaw("Mouse ScrollWheel");
|
var scroll = Input.GetAxisRaw("Mouse ScrollWheel");
|
||||||
if (scroll != 0)
|
if (scroll != 0)
|
||||||
@@ -62,7 +76,11 @@ public class CameraControl : MonoBehaviour
|
|||||||
|
|
||||||
// Move towards the target zoom
|
// Move towards the target zoom
|
||||||
if (_targetZoom != camera.orthographicSize)
|
if (_targetZoom != camera.orthographicSize)
|
||||||
|
if (!isPanning)
|
||||||
camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, _targetZoom, Time.deltaTime * TargetZoomMultiplier);
|
camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, _targetZoom, Time.deltaTime * TargetZoomMultiplier);
|
||||||
|
else
|
||||||
|
// panning cancels target zoom immediately
|
||||||
|
_targetZoom = camera.orthographicSize;
|
||||||
|
|
||||||
// Move towards the target position
|
// Move towards the target position
|
||||||
if (_targetPosition != (Vector2)transform.position)
|
if (_targetPosition != (Vector2)transform.position)
|
||||||
@@ -71,4 +89,24 @@ public class CameraControl : MonoBehaviour
|
|||||||
transform.position = new Vector3(newPosition.x, newPosition.y, -20);
|
transform.position = new Vector3(newPosition.x, newPosition.y, -20);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (_panningClickPosition == null)
|
||||||
|
{
|
||||||
|
_panningClickPosition = Input.mousePosition;
|
||||||
|
_panningCameraPosition = transform.position;
|
||||||
|
}
|
||||||
|
// We need to apply the delta between the initial click position and the current mouse position to the camera's position
|
||||||
|
var worldDelta = camera.ScreenToWorldPoint(Input.mousePosition) - camera.ScreenToWorldPoint(_panningClickPosition.Value);
|
||||||
|
Debug.Log($"worldDelta: {worldDelta} screenDelta: {(Vector2)Input.mousePosition - _panningClickPosition.Value}");
|
||||||
|
|
||||||
|
transform.position = new Vector3(
|
||||||
|
_panningCameraPosition.Value.x - worldDelta.x,
|
||||||
|
_panningCameraPosition.Value.y - worldDelta.y,
|
||||||
|
-20
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user