using Unity.Netcode; using UnityEngine; using UnityEngine.InputSystem; /// /// Server-authoritative prototype movement. /// The owner reads input, the server applies it, and NetworkTransform replicates the result. /// [RequireComponent(typeof(NetworkObject))] public sealed class FriendslopPlayerMotor : NetworkBehaviour { [SerializeField, Min(0.1f)] private float moveSpeed = 4.5f; private void Update() { if (!IsOwner || !IsSpawned || Keyboard.current == null) { return; } Vector2 input = ReadMoveInput(); if (IsServer) { MoveOnServer(input); } else { MoveServerRpc(input); } } [ServerRpc] private void MoveServerRpc(Vector2 input) { MoveOnServer(input); } private void MoveOnServer(Vector2 input) { if (input.sqrMagnitude > 1f) { input.Normalize(); } Vector3 direction = new Vector3(input.x, 0f, input.y); transform.position += direction * (moveSpeed * Time.deltaTime); if (direction.sqrMagnitude > 0.001f) { transform.forward = Vector3.Slerp(transform.forward, direction, 14f * Time.deltaTime); } } private static Vector2 ReadMoveInput() { Keyboard keyboard = Keyboard.current; float horizontal = (keyboard.dKey.isPressed || keyboard.rightArrowKey.isPressed ? 1f : 0f) - (keyboard.aKey.isPressed || keyboard.leftArrowKey.isPressed ? 1f : 0f); float vertical = (keyboard.wKey.isPressed || keyboard.upArrowKey.isPressed ? 1f : 0f) - (keyboard.sKey.isPressed || keyboard.downArrowKey.isPressed ? 1f : 0f); return new Vector2(horizontal, vertical); } }