rimworld-animation-studio/Assets/Scripts/Managers/CameraController.cs
2022-09-17 19:06:33 -05:00

96 lines
2.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RimWorldAnimationStudio
{
public class CameraController : MonoBehaviour
{
private Camera cam;
[Header("Scroll controls")]
public float scrollSpeed = 100f;
[Header("Zoom controls")]
public float zoom = -5f;
public float minZoom = -3f;
public float maxZoom = -15f;
[Header("Max bounds")]
public float maxBoundsXAxis = 30;
public float maxBoundsYAxis = 30;
private float x;
private float y;
private float curZoom;
private bool mouseDragActive = false;
private Vector3 mouseDragOrigin;
private void Start()
{
x = transform.position.x;
y = transform.position.y;
curZoom = zoom;
transform.position = new Vector3(transform.position.x, transform.position.y, -10);
cam = this.GetComponent<Camera>();
}
private void Update()
{
if (Input.GetMouseButton(2))
{ StartMouseDrag(); }
else if (Input.GetMouseButtonUp(2))
{ ResetMouseDrag(); }
curZoom += Input.GetAxis("Mouse ScrollWheel") * scrollSpeed * 0.1f;
curZoom = Mathf.Clamp(curZoom, maxZoom, minZoom);
Vector3 cameraPosition = Vector3.Lerp(transform.position, new Vector3(x, y, -10), 0.2f);
transform.position = cameraPosition;
cam.orthographicSize = Mathf.Abs(curZoom);
}
public void SetPosition(Vector3 position)
{
x = position.x;
y = position.y;
transform.position = position;
}
public void SetZoom(float zoom)
{
this.zoom = Mathf.Clamp(zoom, maxZoom, minZoom);
}
public void StartMouseDrag()
{
Vector3 delta = cam.ScreenToWorldPoint(Input.mousePosition) - cam.transform.position;
if (mouseDragActive == false)
{
mouseDragActive = true;
mouseDragOrigin = cam.ScreenToWorldPoint(Input.mousePosition);
}
cam.transform.position = mouseDragOrigin - delta;
}
public void ResetMouseDrag()
{
mouseDragActive = false;
}
public void ResetCamera()
{
cam.transform.position = new Vector3(0, 0, -10);
curZoom = zoom;
mouseDragActive = false;
}
}
}