Added leveler
This commit is contained in:
@@ -5,12 +5,20 @@ public sealed class FirstPersonCameraController : NetworkBehaviour, IPlayerSyste
|
||||
{
|
||||
[SerializeField, Range(1f, 89f)] private float verticalLookLimit = 80f;
|
||||
|
||||
private readonly NetworkVariable<float> synchronizedPitch = new(
|
||||
0f,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Owner);
|
||||
|
||||
private Camera playerCamera;
|
||||
private AudioListener audioListener;
|
||||
private MeshRenderer playerRenderer;
|
||||
private float pitch;
|
||||
private bool isInitialized;
|
||||
|
||||
public Transform AimTransform => playerCamera != null ? playerCamera.transform : transform;
|
||||
public Vector3 AimForward => AimTransform.forward;
|
||||
|
||||
public void Initialize(PlayerFacade facade)
|
||||
{
|
||||
playerCamera = GetComponentInChildren<Camera>(true);
|
||||
@@ -24,6 +32,9 @@ public sealed class FirstPersonCameraController : NetworkBehaviour, IPlayerSyste
|
||||
bool isLocalPlayer = IsOwner;
|
||||
playerCamera.gameObject.SetActive(isLocalPlayer);
|
||||
|
||||
if (!isLocalPlayer)
|
||||
ApplyCameraPitch(synchronizedPitch.Value);
|
||||
|
||||
if (!isLocalPlayer)
|
||||
return;
|
||||
|
||||
@@ -43,21 +54,48 @@ public sealed class FirstPersonCameraController : NetworkBehaviour, IPlayerSyste
|
||||
Cursor.visible = false;
|
||||
}
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
synchronizedPitch.OnValueChanged += HandleSynchronizedPitchChanged;
|
||||
|
||||
if (isInitialized && !IsOwner)
|
||||
ApplyCameraPitch(synchronizedPitch.Value);
|
||||
}
|
||||
|
||||
public void ApplyPitch(float pitchDelta)
|
||||
{
|
||||
if (!isInitialized || !IsOwner)
|
||||
return;
|
||||
|
||||
pitch = Mathf.Clamp(pitch - pitchDelta, -verticalLookLimit, verticalLookLimit);
|
||||
playerCamera.transform.localRotation = Quaternion.Euler(pitch, 0f, 0f);
|
||||
ApplyCameraPitch(pitch);
|
||||
|
||||
if (IsSpawned)
|
||||
synchronizedPitch.Value = pitch;
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
synchronizedPitch.OnValueChanged -= HandleSynchronizedPitchChanged;
|
||||
|
||||
if (IsOwner)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleSynchronizedPitchChanged(float _, float currentPitch)
|
||||
{
|
||||
if (!IsOwner)
|
||||
ApplyCameraPitch(currentPitch);
|
||||
}
|
||||
|
||||
private void ApplyCameraPitch(float currentPitch)
|
||||
{
|
||||
pitch = currentPitch;
|
||||
|
||||
if (playerCamera != null)
|
||||
playerCamera.transform.localRotation = Quaternion.Euler(pitch, 0f, 0f);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user