63 lines
1.7 KiB
C#
63 lines
1.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class GlobeRotator : MonoBehaviour
|
|
{
|
|
public float rotationSpeed = 200f;
|
|
public float zoomSpeed = 2f;
|
|
public float minZoom = 5f;
|
|
public float maxZoom = 25f;
|
|
public float smoothing = 8f;
|
|
|
|
private Vector3 _lastMousePos;
|
|
private bool _isDragging;
|
|
private Camera _cam;
|
|
private float _deltaX;
|
|
private float _deltaY;
|
|
|
|
void Start()
|
|
{
|
|
_cam = Camera.main;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (Input.GetMouseButtonDown(1))
|
|
{
|
|
_lastMousePos = Input.mousePosition;
|
|
_isDragging = true;
|
|
}
|
|
|
|
if (Input.GetMouseButtonUp(1))
|
|
_isDragging = false;
|
|
|
|
float targetX = 0f;
|
|
float targetY = 0f;
|
|
|
|
if (_isDragging)
|
|
{
|
|
Vector3 delta = Input.mousePosition - _lastMousePos;
|
|
targetX = -delta.x * rotationSpeed * Time.deltaTime;
|
|
targetY = delta.y * rotationSpeed * Time.deltaTime;
|
|
_lastMousePos = Input.mousePosition;
|
|
}
|
|
|
|
_deltaX = Mathf.Lerp(_deltaX, targetX, Time.deltaTime * smoothing);
|
|
_deltaY = Mathf.Lerp(_deltaY, targetY, Time.deltaTime * smoothing);
|
|
|
|
transform.Rotate(Vector3.up, _deltaX, Space.World);
|
|
|
|
Vector3 rightAxis = _cam.transform.right;
|
|
transform.Rotate(rightAxis, _deltaY, Space.World);
|
|
|
|
float scroll = Input.GetAxis("Mouse ScrollWheel");
|
|
if (scroll != 0f && _cam != null)
|
|
{
|
|
float dist = Vector3.Distance(_cam.transform.position, transform.position);
|
|
dist = Mathf.Clamp(dist - scroll * zoomSpeed, minZoom, maxZoom);
|
|
_cam.transform.position = transform.position - _cam.transform.forward * dist;
|
|
}
|
|
}
|
|
}
|