91 lines
3.1 KiB
C#
91 lines
3.1 KiB
C#
using Unity.Netcode;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
[RequireComponent(typeof(CharacterController))]
|
|
public sealed class DeveloperPlayerController : MonoBehaviour
|
|
{
|
|
[SerializeField, Min(0.1f)] private float walkSpeed = 5f;
|
|
[SerializeField, Min(0.1f)] private float flySpeed = 12f;
|
|
[SerializeField, Min(0.01f)] private float mouseSensitivity = 0.08f;
|
|
[SerializeField, Min(0.1f)] private float gravity = 20f;
|
|
[SerializeField, Range(1f, 89f)] private float verticalLookLimit = 85f;
|
|
|
|
private CharacterController characterController;
|
|
private Camera playerCamera;
|
|
private float verticalVelocity;
|
|
private float pitch;
|
|
private bool isFlying;
|
|
|
|
private void Awake()
|
|
{
|
|
if (NetworkManager.Singleton != null && NetworkManager.Singleton.IsListening)
|
|
{
|
|
gameObject.SetActive(false);
|
|
return;
|
|
}
|
|
|
|
characterController = GetComponent<CharacterController>();
|
|
playerCamera = GetComponentInChildren<Camera>(true);
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
|
|
foreach (Camera camera in FindObjectsByType<Camera>(FindObjectsSortMode.None))
|
|
if (camera != playerCamera) camera.enabled = false;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Keyboard.current == null)
|
|
return;
|
|
|
|
if (Keyboard.current.fKey.wasPressedThisFrame)
|
|
isFlying = !isFlying;
|
|
|
|
UpdateLook();
|
|
UpdateMovement();
|
|
}
|
|
|
|
private void UpdateLook()
|
|
{
|
|
if (Mouse.current == null)
|
|
return;
|
|
|
|
Vector2 mouseDelta = Mouse.current.delta.ReadValue() * mouseSensitivity;
|
|
transform.Rotate(0f, mouseDelta.x, 0f, Space.Self);
|
|
pitch = Mathf.Clamp(pitch - mouseDelta.y, -verticalLookLimit, verticalLookLimit);
|
|
playerCamera.transform.localRotation = Quaternion.Euler(pitch, 0f, 0f);
|
|
}
|
|
|
|
private void UpdateMovement()
|
|
{
|
|
Vector2 input = new(
|
|
(Keyboard.current.dKey.isPressed ? 1f : 0f) - (Keyboard.current.aKey.isPressed ? 1f : 0f),
|
|
(Keyboard.current.wKey.isPressed ? 1f : 0f) - (Keyboard.current.sKey.isPressed ? 1f : 0f));
|
|
if (input.sqrMagnitude > 1f)
|
|
input.Normalize();
|
|
|
|
float speed = Keyboard.current.leftShiftKey.isPressed ? flySpeed : walkSpeed;
|
|
Vector3 direction = transform.right * input.x + transform.forward * input.y;
|
|
|
|
if (isFlying)
|
|
{
|
|
direction += Vector3.up * ((Keyboard.current.spaceKey.isPressed ? 1f : 0f) -
|
|
(Keyboard.current.leftCtrlKey.isPressed ? 1f : 0f));
|
|
characterController.Move(direction.normalized * speed * Time.deltaTime);
|
|
return;
|
|
}
|
|
|
|
if (characterController.isGrounded && verticalVelocity < 0f)
|
|
verticalVelocity = -2f;
|
|
verticalVelocity -= gravity * Time.deltaTime;
|
|
characterController.Move((direction * speed + Vector3.up * verticalVelocity) * Time.deltaTime);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
}
|
|
}
|