mirror of
https://github.com/Xevion/Rebirth.git
synced 2025-12-08 14:08:07 -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,42 +35,78 @@ public class CameraControl : MonoBehaviour
|
|||||||
|
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
// calculate target zoom
|
var isPanning = Input.GetMouseButton(1);
|
||||||
var scroll = Input.GetAxisRaw("Mouse ScrollWheel");
|
|
||||||
if (scroll != 0)
|
if (!isPanning)
|
||||||
{
|
{
|
||||||
// scroll passes on it's negative value
|
if (_panningClickPosition != null)
|
||||||
var zoomAmount = camera.orthographicSize * scroll * ZoomSpeed;
|
{
|
||||||
// changes the target zoom, keeps it within min/max range
|
_panningClickPosition = null;
|
||||||
_targetZoom = Mathf.Clamp(camera.orthographicSize - zoomAmount, MinZoom, MaxZoom);
|
_panningCameraPosition = null;
|
||||||
|
_targetPosition = (Vector2)transform.position;
|
||||||
|
_targetZoom = camera.orthographicSize;
|
||||||
|
}
|
||||||
|
|
||||||
// Camera.main.transform.position = Vector2.Lerp(Camera.main.transform.position,
|
// calculate target zoom
|
||||||
// Camera.main.ScreenToWorldPoint(Input.mousePosition),
|
var scroll = Input.GetAxisRaw("Mouse ScrollWheel");
|
||||||
// ZoomSpeed * Time.deltaTime);
|
if (scroll != 0)
|
||||||
|
{
|
||||||
|
// scroll passes on it's negative value
|
||||||
|
var zoomAmount = camera.orthographicSize * scroll * ZoomSpeed;
|
||||||
|
// changes the target zoom, keeps it within min/max range
|
||||||
|
_targetZoom = Mathf.Clamp(camera.orthographicSize - zoomAmount, MinZoom, MaxZoom);
|
||||||
|
|
||||||
|
// Camera.main.transform.position = Vector2.Lerp(Camera.main.transform.position,
|
||||||
|
// Camera.main.ScreenToWorldPoint(Input.mousePosition),
|
||||||
|
// ZoomSpeed * Time.deltaTime);
|
||||||
|
|
||||||
|
|
||||||
// //KEEP THE Z POSITION BEHIND YOUR OBJECTS!
|
// //KEEP THE Z POSITION BEHIND YOUR OBJECTS!
|
||||||
// Camera.main.transform.position = new Vector3(Camera.main.transform.position.x,
|
// Camera.main.transform.position = new Vector3(Camera.main.transform.position.x,
|
||||||
// Camera.main.transform.position.y,
|
// Camera.main.transform.position.y,
|
||||||
// -20);
|
// -20);
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate target position
|
||||||
|
// moveSpeed dependent on zoom, moves slower when zoomed in
|
||||||
|
// 50% speed at 2.5 zoom, 100% speed at 5 zoom, 200% speed at 10 zoom
|
||||||
|
// 0.5 = 1 / (5 / 2.5), 1 = (5 / 5), 2 = (5 / 10)
|
||||||
|
var movement = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * MoveSpeed * Time.deltaTime * (1 / (5 / camera.orthographicSize));
|
||||||
|
_targetPosition += (Vector2)movement;
|
||||||
|
|
||||||
|
// Move towards the target zoom
|
||||||
|
if (_targetZoom != camera.orthographicSize)
|
||||||
|
if (!isPanning)
|
||||||
|
camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, _targetZoom, Time.deltaTime * TargetZoomMultiplier);
|
||||||
|
else
|
||||||
|
// panning cancels target zoom immediately
|
||||||
|
_targetZoom = camera.orthographicSize;
|
||||||
|
|
||||||
|
// Move towards the target position
|
||||||
|
if (_targetPosition != (Vector2)transform.position)
|
||||||
|
{
|
||||||
|
var newPosition = Vector2.Lerp(transform.position, _targetPosition, Time.deltaTime * TargetPositionMultiplier);
|
||||||
|
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
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// calculate target position
|
|
||||||
// moveSpeed dependent on zoom, moves slower when zoomed in
|
|
||||||
// 50% speed at 2.5 zoom, 100% speed at 5 zoom, 200% speed at 10 zoom
|
|
||||||
// 0.5 = 1 / (5 / 2.5), 1 = (5 / 5), 2 = (5 / 10)
|
|
||||||
var movement = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * MoveSpeed * Time.deltaTime * (1 / (5 / camera.orthographicSize));
|
|
||||||
_targetPosition += (Vector2)movement;
|
|
||||||
|
|
||||||
// Move towards the target zoom
|
|
||||||
if (_targetZoom != camera.orthographicSize)
|
|
||||||
camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, _targetZoom, Time.deltaTime * TargetZoomMultiplier);
|
|
||||||
|
|
||||||
// Move towards the target position
|
|
||||||
if (_targetPosition != (Vector2)transform.position)
|
|
||||||
{
|
|
||||||
var newPosition = Vector2.Lerp(transform.position, _targetPosition, Time.deltaTime * TargetPositionMultiplier);
|
|
||||||
transform.position = new Vector3(newPosition.x, newPosition.y, -20);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user