43 lines
1.5 KiB
C#
43 lines
1.5 KiB
C#
using Unity.Netcode;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
[RequireComponent(typeof(PlayerController), typeof(FirstPersonCameraController))]
|
|
public sealed class PlayerInputController : NetworkBehaviour, IPlayerSystem
|
|
{
|
|
[SerializeField, Min(0.001f)] private float mouseSensitivity = 0.08f;
|
|
|
|
private PlayerController playerController;
|
|
private FirstPersonCameraController cameraController;
|
|
private bool isInitialized;
|
|
|
|
public void Initialize(PlayerFacade facade)
|
|
{
|
|
playerController = GetComponent<PlayerController>();
|
|
cameraController = GetComponent<FirstPersonCameraController>();
|
|
isInitialized = playerController != null && cameraController != null;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!isInitialized || !IsOwner || Keyboard.current == null)
|
|
return;
|
|
|
|
Vector2 moveInput = 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));
|
|
|
|
playerController.SubmitMovement(
|
|
moveInput,
|
|
Keyboard.current.leftShiftKey.isPressed,
|
|
Keyboard.current.spaceKey.wasPressedThisFrame);
|
|
|
|
if (Mouse.current == null)
|
|
return;
|
|
|
|
Vector2 mouseDelta = Mouse.current.delta.ReadValue() * mouseSensitivity;
|
|
playerController.SubmitYaw(mouseDelta.x);
|
|
cameraController.ApplyPitch(mouseDelta.y);
|
|
}
|
|
}
|