init
This commit is contained in:
58
Assets/Scripts/CameraController.cs
Normal file
58
Assets/Scripts/CameraController.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user