#but the player only spawns on the host

1 messages · Page 1 of 1 (latest)

zealous oasis
#

You'll need to also call Spawn() in that server RPC

sterile basalt
#
[ServerRpc(RequireOwnership = false)]
    public void SetupPlayerServerRpc(int _selectedPlayerID)
    {
        MyDebugger.myDebugger.Log("Setup Player Server Rpc");
        GameObject playerClone;
        MyDebugger.myDebugger.Log($"Prefab List " + plrefablist);
        for (int i = 0; i < plrefablist.PrefabList.Count; i++)
        {
            if (i == _selectedPlayerID)
            {
                playerClone = Instantiate(plrefablist.PrefabList[i].Prefab);

                NetworkObject playerNetObj;
                playerNetObj = playerClone.GetComponent<NetworkObject>();
                playerNetObj.Spawn();

                MyDebugger.myDebugger.Log($"Prefab Name " + plrefablist.PrefabList[i].Prefab);
            }
        } 
    }
#

this spawns the player on the host and i can move the player but if i try and sawn the client player it only spawns on the host side and the host controls both the host and client player. nothing happens on the client side.

zealous oasis
#

Spawn() should create an object on the client as well.
You'll need to check your movement code so that only the local client is moving the player. the object will need a client network transform if your movement is client authoratative

sterile basalt
#

shouldn't IsOwner handle that?

zealous oasis
#

It should. depends on where you are checking it

sterile basalt
#

why dose it look like its doing something twice?

zealous oasis
#

I think OnNetworkSpawn() runs on the host server and the host client. So on the host it ends up running twice

sterile basalt
#

any suggestions?

zealous oasis
#

just something like if(IsServer) return;

sterile basalt
#

brb, family time

sterile basalt
#

ok im back

#

have you tested this to see if you can get it to work?

zealous oasis
#

Nope. I'm working on netcode for entities at the moment

sterile basalt
#

It still dosnt work but what are your thoghts on this?

 public override void OnNetworkSpawn()
    {
        MyDebugger.myDebugger.Log("Setup Player Server Rpc");

        if (IsServer)
        {
            MyDebugger.myDebugger.Log("On Network Spawn - Is Server");
            //return;
        }

        if (IsClient) {
            MyDebugger.myDebugger.Log("On Network Spawn - Is Client");
        }

        if (IsHost) {
            MyDebugger.myDebugger.Log("On Network Spawn - Is Host");
        }
        
        MyDebugger.myDebugger.Log($"Prefab List " + plrefablist);

        for (int i = 0; i < plrefablist.PrefabList.Count; i++)
        {
            if (i == selectedPlayerID)
            {
                playerClone = Instantiate(plrefablist.PrefabList[i].Prefab);
                playerClones.Add(playerClone);
                
                MyDebugger.myDebugger.Log($"Prefab Name " + plrefablist.PrefabList[i].Prefab);
            }
        }

        SetupPlayerServerRpc();
    }

    [ServerRpc(RequireOwnership = false)]
    private void SetupPlayerServerRpc()
    {
        MyDebugger.myDebugger.Log($"Setup PlayerServer Rpc");

        foreach (GameObject playerClone in playerClones)
        {
            NetworkObject playerNetObj = playerClone.GetComponent<NetworkObject>();
            playerNetObj.Spawn();
            //playerNetObjRef.TryGet(out NetworkObject playerNetObj);
        }

    }
zealous oasis
#

As long as player clones is being set on the server. But at that point you don't really need the serverRPC. It should already be running on the server there. Playerclone.Spawn() should work there

sterile basalt
#

Sorry to get a little off topic for a sec
I tried to add Multiplayer Tools from the package manager but it gave me a small error