55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
using Cinemachine;
|
|
using RebootKit.Engine.Services.Simulation.Sensors;
|
|
using UnityEngine;
|
|
|
|
namespace RebootKit.FPPKit {
|
|
[AddComponentMenu(FPPConsts.k_AddComponentMenu + "First Person Camera")]
|
|
public class FPPCamera : MonoBehaviour {
|
|
[SerializeField] float _pickDistance = 5.0f;
|
|
|
|
[SerializeField] LayerMask _pickLayer;
|
|
|
|
[field: SerializeField]
|
|
public float Sensitivity { get; set; }
|
|
|
|
[SerializeField] float _pitchMin = -80f;
|
|
|
|
[SerializeField] float _pitchMax = 80f;
|
|
|
|
[field: SerializeField]
|
|
public CinemachineVirtualCamera Camera { get; private set; }
|
|
|
|
readonly RaycastSensor _raycastSensor = new();
|
|
|
|
public float Pitch { get; private set; }
|
|
public float Yaw { get; private set; }
|
|
|
|
public ISensor Sensor {
|
|
get {
|
|
_raycastSensor.ray = new Ray(Camera.transform.position, Camera.transform.forward);
|
|
_raycastSensor.maxDistance = _pickDistance;
|
|
_raycastSensor.layerMask = _pickLayer;
|
|
return _raycastSensor;
|
|
}
|
|
}
|
|
|
|
void LateUpdate() {
|
|
Camera.transform.localRotation = Quaternion.Euler(Pitch, 0f, 0f);
|
|
transform.rotation = Quaternion.Euler(0f, Yaw, 0f);
|
|
}
|
|
|
|
public void Rotate(float x, float y) {
|
|
float sens = Sensitivity;
|
|
|
|
Pitch -= y * sens;
|
|
Pitch = Mathf.Clamp(Pitch, _pitchMin, _pitchMax);
|
|
|
|
Yaw += x * sens;
|
|
}
|
|
|
|
public void SetLookDirection(Vector3 forward) {
|
|
Pitch = Mathf.Asin(-forward.y) * Mathf.Rad2Deg;
|
|
Yaw = Mathf.Atan2(forward.x, forward.z) * Mathf.Rad2Deg;
|
|
}
|
|
}
|
|
} |