0
\$\begingroup\$

I am using this Kinematic Character Control package I setup some Character Controls and all looks like it works fine in local. I am trying to setup some basic Networking.

I installed Network for Gameobject and did the following on my scripts


using Unity.Netcode;
using UnityEngine;

public struct PlayerCharacterInputs : INetworkSerializable
{
    public float MoveAxisForward;
    public float MoveAxisRight;
    public float LookAxisUp;
    public float LookAxisRight;
    public bool Jump;
    public bool Crouch;

    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    {
        serializer.SerializeValue(ref MoveAxisForward);
        serializer.SerializeValue(ref MoveAxisRight);
        serializer.SerializeValue(ref LookAxisUp);
        serializer.SerializeValue(ref LookAxisRight);
        serializer.SerializeValue(ref Jump);
        serializer.SerializeValue(ref Crouch);
    }
}

[RequireComponent(typeof(VirtualController))]
public class PlayerInputHandler : NetworkBehaviour
{
    private InputManager inputManager;
    private VirtualController virtualController;

    private void Awake()
    {
        inputManager = InputManager.Instance;
        virtualController = GetComponent<VirtualController>();
    }

    // Update is called once per frame
    void Update()
    {
        if (!IsOwner) return;
        PlayerCharacterInputs inputs = GetInputs();
        virtualController.UpdateInputs(inputs);
        SendInputToServerServerRpc(inputs);
    }

    [ServerRpc]
    public void SendInputToServerServerRpc(PlayerCharacterInputs inputs)
    {
        virtualController.UpdateInputs(inputs);
    }
   
    public void ProcessInputsOnServer(PlayerCharacterInputs inputs)
    {
        virtualController.UpdateInputs(inputs);
    }

    public PlayerCharacterInputs GetInputs()
    {
        return new PlayerCharacterInputs
        {
            MoveAxisForward = inputManager.GetMovement().x,
            MoveAxisRight = inputManager.GetMovement().y,
            LookAxisRight = inputManager.GetLook().x,
            LookAxisUp = inputManager.GetLook().y,
            Jump = inputManager.IsJumpingThisFrame(),
            Crouch = inputManager.IsCrouching()
        };
    }
}

Basically, I send all input from client to the server, and I let the server execute the same logic on the motor. I am totally unsure if it s correct approach for now, but it s how I would like to start setting up things.

Now, I also setup the Player Gameobject like this Screenshot 2024-02-20 211225|389x500

On the host, I can move and see both player move, on the client, I only see the client move not the host. Also, on the host, the starting position and after movement position is completely off, the movement are fine, but the final position is completely off.

I am unsure why, from my understanding, I should be able to process the input on the server, move the transform and resync the client with a treshold position if it missmatch. But currently nothing sync at all.

Do you have potential idea for fixing ?

EDIT : I have my network player in the NetworkManager's "Player Prefab" field

\$\endgroup\$
2
  • \$\begingroup\$ I'm not sure you actually need to use the ServerRpc for movement since that should be the job on the Network Transform component. \$\endgroup\$
    – Zibelas
    Feb 21 at 6:49
  • \$\begingroup\$ Network Transform (NT) does nothing at all on the player somehow. if I dont use ServerRcp, then my players only move locally. I tried to move some gameobject in the scene without the Kinematic Character Controller (KCC) but a NT, and it sync properly, so I am unsure what is preventing the NT to work in this case. \$\endgroup\$
    – Crocsx
    Feb 21 at 11:00

0

You must log in to answer this question.

Browse other questions tagged .