- Home /
NetworkManager class isLocalPlayer equivalent?
Hi all,
I have a question about how the network instantiation of prefabs works.
I'm using a dedicated server, so the host for networking is always my machine.
What I'm trying to achieve is this:
Spawn a prefab on the server that represents a newly connected client (player1) on the server side.
Spawn the same prefab on all clients in order to represent player1 in their scene.
Spawn a different prefab in player1's scene to represent themselves.
The same would be true of all players connecting: a prefab representing themselves in their own scene and a different prefab representing all other players.
In the NetworkBehaviour class I would usually use isLocalPlayer on the player gameobject but of course that public bool doesn't exist because we haven't yet created the local player object.
Is there an isLocalPlayer concept available for use within the NetworkManager class? It seems to me that whilst I can use a public override void OnServerAddPlayer to spawn a prefab on the server and then another override for OnStartClient to spawn prefabs in client scenes, I can't distinguish between a localplayer spawning on the client versus a spawning of a different player's prefab. Below is the code I currently have so you can see what the context is.
Help on this would be much appreciated.
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
GameObject player = (GameObject)Instantiate(remotePlayer, Vector3.zero, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
public override void OnStartClient(NetworkClient client) {
ClientScene.RegisterSpawnHandler(remotePlayer.GetComponent<NetworkIdentity>().assetId, SpawnPlayer, UnspawnPlayer);
base.OnStartClient(client);
uMMO.get.architectureToCompile = CompilationArchitecture.Client;
}
GameObject SpawnPlayer(Vector3 position, NetworkHash128 assetID)
{
// How do I know if I'm instantiating a gameobject for the localplayer of a client or if it's a remote player on the local client?
GameObject _object = Instantiate(playerPrefab);
return _object;
}
void UnspawnPlayer(GameObject inObject)
{
Destroy(inObject);
}
@seanr I noticed another thread that you were able to help solve, along similar lines. Would you $$anonymous$$d having a look at the above? Would be very grateful for any advice.
Your answer
Follow this Question
Related Questions
Failure generating network code 1 Answer
what the gameobject belong to if networkidentity do not choose two checkbox ? 0 Answers
networking, online FPS player invisible to others after respawn 0 Answers
Instantiate By ID? 0 Answers
Network.Instantiate/Destroy vs Instantiate/Destroy on each client using [Command] and [ClientRpc] 1 Answer