109 lines
2.6 KiB
C#
109 lines
2.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class CameraOrbit : MonoBehaviour
|
|
{
|
|
public static CameraOrbit Instance;
|
|
bool controlOn;
|
|
|
|
EventSystem eventSystem;
|
|
|
|
[Header("Orbit Camera")]
|
|
public float CamZoomMin = 1, CamZoomMax = 15;
|
|
|
|
public float CamZoom = 2.4f;
|
|
public float MoveSpeed = 1;
|
|
public float ZoomSpeed = 1f;
|
|
public Camera Camera;
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
Camera = GetComponent<Camera>();
|
|
eventSystem = EventSystem.current;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
OrbitAround(false);
|
|
}
|
|
|
|
float baseZoom;
|
|
float baseDist;
|
|
Vector2 lastMouse;
|
|
Vector2 lastTouch;
|
|
int lastTouchCount;
|
|
bool lastControlOn;
|
|
|
|
void OrbitAround(bool aroundCharacter = false)
|
|
{
|
|
Vector2 mouse = Input.mousePosition;
|
|
Vector2 mouseDelta = Vector2.zero;
|
|
Vector2 touchDelta = Vector2.zero;
|
|
Vector3 position = transform.position;
|
|
|
|
if (Input.GetMouseButtonDown(0) && Input.touchCount == 0 && !eventSystem.IsPointerOverGameObject())
|
|
{
|
|
Debug.Log("Mouse");
|
|
lastControlOn = false;
|
|
controlOn = true;
|
|
}
|
|
if (Input.GetMouseButton(0) && controlOn)
|
|
{
|
|
if(lastControlOn == false)
|
|
{
|
|
lastControlOn = true;
|
|
mouseDelta = Vector2.zero;
|
|
}
|
|
else
|
|
{
|
|
mouseDelta = (mouse - lastMouse) * Time.deltaTime;
|
|
}
|
|
position -= mouseDelta.x * MoveSpeed * transform.right;
|
|
position -= mouseDelta.y * MoveSpeed * transform.up;
|
|
position.x = Mathf.Clamp(position.x, -5, 5);
|
|
position.y = Mathf.Clamp(position.y, -5, 5);
|
|
lastMouse = mouse;
|
|
}
|
|
else
|
|
{
|
|
controlOn = false;
|
|
}
|
|
|
|
if (!eventSystem.IsPointerOverGameObject())
|
|
{
|
|
SetZoom(CamZoom - Input.mouseScrollDelta.y * ZoomSpeed);
|
|
}
|
|
|
|
float camDist = CamZoom;
|
|
Camera.orthographicSize = camDist;
|
|
transform.position = position;
|
|
}
|
|
|
|
public void SetZoom(string value)
|
|
{
|
|
SetZoom(float.Parse(value, CultureInfo.InvariantCulture));
|
|
}
|
|
|
|
public void SetZoom(float value)
|
|
{
|
|
value = Mathf.Round(value * 1000) / 1000;
|
|
CamZoom = Mathf.Clamp(value, CamZoomMin, CamZoomMax);
|
|
}
|
|
|
|
public void SetZoomSpeed(float value)
|
|
{
|
|
ZoomSpeed = value;
|
|
}
|
|
|
|
public void SetMoveSpeed(float value)
|
|
{
|
|
MoveSpeed = value;
|
|
}
|
|
}
|