wip: PlayerCamera
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.InputSystem;
|
using UnityEngine.InputSystem;
|
||||||
|
|
||||||
|
[ExecuteInEditMode]
|
||||||
public class PlayerCamera : MonoBehaviour
|
public class PlayerCamera : MonoBehaviour
|
||||||
{
|
{
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
@@ -42,11 +43,8 @@ public class PlayerCamera : MonoBehaviour
|
|||||||
public Transform Target;
|
public Transform Target;
|
||||||
}
|
}
|
||||||
|
|
||||||
[SerializeField]
|
[SerializeField] private Settings _settings;
|
||||||
private Settings _settings;
|
[SerializeField] private References _references;
|
||||||
|
|
||||||
[SerializeField]
|
|
||||||
private References _references;
|
|
||||||
|
|
||||||
private float _yaw;
|
private float _yaw;
|
||||||
private float _pitch;
|
private float _pitch;
|
||||||
@@ -54,9 +52,13 @@ public class PlayerCamera : MonoBehaviour
|
|||||||
private Vector3 _playerPosition;
|
private Vector3 _playerPosition;
|
||||||
private InputAction _lookAction;
|
private InputAction _lookAction;
|
||||||
|
|
||||||
void OnEnable()
|
void Awake()
|
||||||
{
|
{
|
||||||
_lookAction = _references.InputActions.FindActionMap("Player").FindAction("Look");
|
_lookAction = _references.InputActions.FindActionMap("Player").FindAction("Look");
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnEnable()
|
||||||
|
{
|
||||||
_lookAction?.Enable();
|
_lookAction?.Enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,21 +73,54 @@ public class PlayerCamera : MonoBehaviour
|
|||||||
|
|
||||||
SetCursor();
|
SetCursor();
|
||||||
SetYawAndPitch(t);
|
SetYawAndPitch(t);
|
||||||
SetPosition();
|
SetPosition(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetCursor()
|
private void SetCursor()
|
||||||
{
|
{
|
||||||
|
if (!Application.isPlaying)
|
||||||
|
return;
|
||||||
|
|
||||||
|
bool lockCursor = !Player.Instance.State.IsPaused;
|
||||||
|
|
||||||
|
Cursor.lockState = lockCursor ? CursorLockMode.Locked : CursorLockMode.None;
|
||||||
|
Cursor.visible = !lockCursor;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetYawAndPitch(float deltaTime)
|
private void SetYawAndPitch(float deltaTime)
|
||||||
{
|
{
|
||||||
|
if (Player.Instance && Player.Instance.State.IsPaused)
|
||||||
|
return;
|
||||||
|
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
if ((Mouse.current.position.ReadValue() - new Vector2(Screen.width, Screen.height) * .5f).magnitude > 10)
|
||||||
|
return;
|
||||||
|
#else
|
||||||
|
if (!Application.isFocused) return;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Vector2 lookInput = _lookAction?.ReadValue<Vector2>() ?? Vector2.zero;
|
||||||
|
|
||||||
|
_pitch -= lookInput.y * _settings.LookSensitivity * deltaTime;
|
||||||
|
_pitch = Mathf.Clamp(_pitch, _settings.MinPitch, _settings.MaxPitch);
|
||||||
|
|
||||||
|
_yaw += lookInput.x * _settings.LookSensitivity * deltaTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetPosition()
|
private void SetPosition(float deltaTime)
|
||||||
{
|
{
|
||||||
|
float t = (1.1f - _settings.FollowSmoothness) * 20 * deltaTime;
|
||||||
|
_playerPosition = Vector3.Lerp(_playerPosition, _references.Target.position, t);
|
||||||
|
|
||||||
|
Vector3 camPos = Vector3.back * _settings.Distance;
|
||||||
|
camPos = Quaternion.Euler(_pitch, _yaw, 0) * camPos;
|
||||||
|
camPos += _playerPosition;
|
||||||
|
|
||||||
|
Quaternion camRot = Quaternion.LookRotation(_playerPosition - camPos);
|
||||||
|
|
||||||
|
camPos.y += _settings.VerticalOffset;
|
||||||
|
|
||||||
|
transform.position = camPos;
|
||||||
|
transform.rotation = camRot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user