using Unity.Netcode;
using UnityEngine;

public class PlayerController : NetworkBehaviour
{
    // This method is called by the client but executed on the server
    [ServerRpc]
    public void MovePlayerServerRpc(Vector3 newPosition)
    {
        // Only the server can execute this logic
        if (IsServer)
        {
            // Update the player's position
            transform.position = newPosition;

            // Synchronize the new position with all clients
            MovePlayerClientRpc(newPosition);
        }
    }

    // This method is called by the server and executed on all clients
    [ClientRpc]
    public void MovePlayerClientRpc(Vector3 newPosition)
    {
        // Update the player's position on all clients
        transform.position = newPosition;
    }

    private void Update()
    {
        // Only the local player can send input
        if (IsOwner)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                // Call the ServerRpc to request a move
                MovePlayerServerRpc(new Vector3(0, 2, 0));
            }
        }
    }
}