1
0
Fork 0
LaboratoryProtection/Assets/UnityTest/Player/Scripts/FirstPersonLogic.cs

162 lines
4.1 KiB
C#

using Cinemachine;
using PMaker.DependencyInjection;
using StarterAssets;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
using UnityEngine.InputSystem;
public class FirstPersonLogic : SingletonMonobehaviour
{
[SerializeField]
private StarterAssetsInputs _starterAssetsInputs;
public FirstPersonController personController;
public CinemachineVirtualCamera virtualCamera;
public Transform cameraRoot;
private void Reset()
{
this._starterAssetsInputs = this.GetComponentInChildren<StarterAssetsInputs>();
this.personController = GetComponentInChildren<FirstPersonController>();
this.virtualCamera = GetComponentInChildren<CinemachineVirtualCamera>();
this.cameraRoot = this.virtualCamera.Follow;
}
public int min = 20;
public int max = 40;
public int zoomSpeed = 2;
private Vector3 defaultPos;
private Quaternion defaultRot;
private Quaternion defaultCameraRot;
private LensSettings defaultLens;
public Vector3 Position
{
get => this.personController.transform.position;
set => this.personController.transform.position = value;
}
public Vector3 FacingAngle
{
get
{
return new Vector3()
{
x = this.cameraRoot.transform.localRotation.x,
y = this.personController.transform.localRotation.y,
};
}
set
{
this.cameraRoot.transform.localRotation = Quaternion.Euler(value.x, 0.0f, 0.0f);
this.personController.transform.localRotation = Quaternion.Euler(0.0f, value.y, 0.0f);
this.personController.CinemachineTargetPitch = value.x;
}
}
private void Start()
{
defaultPos = this.personController.transform.localPosition;
defaultRot = this.personController.transform.localRotation;
defaultCameraRot = this.cameraRoot.transform.localRotation;
defaultLens = this.virtualCamera.m_Lens;
Debug.Log(defaultPos);
Debug.Log(defaultRot);
this.UpdateAsObservable()
.Subscribe(_ => {
if (Mouse.current.rightButton.isPressed)
{
_starterAssetsInputs.cursorInputForLook = true;
}
else
{
_starterAssetsInputs.cursorInputForLook = false;
_starterAssetsInputs.look = Vector2.zero;
}
})
.AddTo(this);
}
void Update()
{
var value = Mouse.current.scroll.ReadValue();
var lens = virtualCamera.m_Lens;
var targetValue = lens.FieldOfView - zoomSpeed * value.y / 120;
targetValue = Mathf.Clamp(targetValue, min, max);
lens.FieldOfView = targetValue;
virtualCamera.m_Lens = lens;
#if UNITY_EDITOR
if (Keyboard.current[Key.R].wasPressedThisFrame)
{
this.ResetState();
}
#endif
}
public void SetPlayer(bool value)
{
this.enabled = value;
}
public void SetActive(bool value)
{
this.transform.GetChild(0).gameObject.SetActive(value);
this.transform.GetChild(1).gameObject.SetActive(value);
}
public void Begin()
{
this.ResetState();
this.SetActive(true);
}
public void End()
{
this.SetActive(false);
this.ResetState();
}
[ContextMenu("ResetState")]
public void ResetState()
{
virtualCamera.m_Lens = defaultLens;
this.personController.transform.localPosition = defaultPos;
this.personController.transform.localRotation = defaultRot;
this.personController.CinemachineTargetPitch = defaultCameraRot.x;
this.cameraRoot.transform.localRotation = defaultCameraRot;
}
private void OnEnable()
{
personController.enabled = true;
}
private void OnDisable()
{
personController.enabled = false;
}
}
namespace StarterAssets
{
public partial class FirstPersonController
{
public float CinemachineTargetPitch
{
get => this._cinemachineTargetPitch;
set => this._cinemachineTargetPitch = value;
}
}
}