59 lines
1.6 KiB
C#
59 lines
1.6 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class CameraController : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private Camera _cam;
|
|
|
|
[SerializeField]
|
|
private InputAction pressed, axis, scroll;
|
|
|
|
private float speed = 1f;
|
|
private Vector2 rotation;
|
|
private float scrollVal;
|
|
private bool canRotate;
|
|
|
|
// Start is called before the first frame update
|
|
void Awake()
|
|
{
|
|
pressed.Enable();
|
|
axis.Enable();
|
|
scroll.Enable();
|
|
|
|
pressed.performed += _ => { StartCoroutine(Rotate()); };
|
|
pressed.canceled += _ => { canRotate = false; };
|
|
|
|
axis.performed += context => { rotation = context.ReadValue<Vector2>(); };
|
|
axis.canceled += _ => { rotation = Vector3.zero; };
|
|
|
|
scroll.performed += context =>
|
|
{
|
|
scrollVal = context.ReadValue<float>();
|
|
ZoomIn();
|
|
};
|
|
}
|
|
|
|
private void ZoomIn()
|
|
{
|
|
RaycastHit hit;
|
|
if(scrollVal < 0 && Physics.Raycast(_cam.transform.position, transform.forward, out hit, 4))
|
|
return;
|
|
if(scrollVal < 0 || Vector3.Distance(_cam.transform.position, transform.position) < 20)
|
|
_cam.transform.position += -scrollVal * transform.forward;
|
|
}
|
|
|
|
private IEnumerator Rotate()
|
|
{
|
|
canRotate = true;
|
|
while (canRotate)
|
|
{
|
|
rotation *= speed;
|
|
transform.Rotate(Vector3.up, rotation.x);
|
|
transform.Rotate(transform.right, -rotation.y, Space.World);
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|