63 lines
1.8 KiB
C#
63 lines
1.8 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.Scripting.APIUpdating;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class CameraController : MonoBehaviour
|
|
{
|
|
public static CameraController instance;
|
|
public float moveSpeed;
|
|
public float zoomSpeed;
|
|
public float rotateSpeed;
|
|
public float minZoomDist;
|
|
public float maxZoomDist;
|
|
private Camera cam;
|
|
void Awake()
|
|
{
|
|
cam = Camera.main;
|
|
instance = this;
|
|
}
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
Move();
|
|
Zoom();
|
|
Rotate();
|
|
}
|
|
|
|
private void Rotate()
|
|
{
|
|
float leftInput = Input.GetKey(KeyCode.E) ? 1.0f : 0.0f;
|
|
float rightInput = Input.GetKey(KeyCode.Q) ? 1.0f : 0.0f;
|
|
float rotAmount = (rightInput - leftInput) * rotateSpeed * Time.deltaTime;
|
|
transform.Rotate(Vector3.up, rotAmount);
|
|
}
|
|
private void Move()
|
|
{
|
|
float xInput = Input.GetAxisRaw("Horizontal");
|
|
float zInput = Input.GetAxisRaw("Vertical");
|
|
Vector3 dir = transform.forward * zInput + transform.right * xInput;
|
|
transform.position += dir * moveSpeed * Time.deltaTime;
|
|
}
|
|
private void Zoom()
|
|
{
|
|
float scrollInput = Input.GetAxis("Mouse ScrollWheel");
|
|
float dist = Vector3.Distance(transform.position, cam.transform.position);
|
|
if(dist < minZoomDist && scrollInput > 0.0f)
|
|
return;
|
|
else if(dist > maxZoomDist && scrollInput < 0.0f)
|
|
return;
|
|
cam.transform.position += cam.transform.forward * scrollInput * zoomSpeed;
|
|
}
|
|
public void FocusOnPosition(Vector3 pos)
|
|
{
|
|
transform.position = pos;
|
|
}
|
|
}
|