1

Sorry if this is a duplicate question.

I found this Reddit post which is exactly my current problem. https://www.reddit.com/r/Unity2D/comments/lw96s5/multiplayer_cameras/

However, there's only one comment which isn't that descriptive and doesn't have any links to the 'tutorial'. To rephrase, I have a MLAPI game in Unity with a LocalPlayer prefab(has a camera, FPS movement code, and PlayerInput) and a ServerPlayer(does not have a camera or PlayerInput component) prefab. They represent different replicated players in my game.

The current problem is the code to decide whether to spawn a local player or a remote player.

Deciding what prefab to spawn:

        private void ApprovalCheck(byte[] connectionData, ulong clientId, MLAPI.NetworkManager.ConnectionApprovedDelegate callback)
        {
            //Your logic here
            bool approve = connectionData == System.Text.Encoding.ASCII.GetBytes("xd");
            bool createPlayerObject = true;

            if (NetworkManager.Singleton.ConnectedClients[clientId].PlayerObject.IsLocalPlayer)
            {
                localPlayerPrefab.GetComponent<NetworkObject>()
                .SpawnAsPlayerObject(clientId);
            }
            else
            {
                serverPlayerPrefab.GetComponent<NetworkObject>()
                .SpawnAsPlayerObject(clientId);
            }


            //If approve is true, the connection gets added. If it's false. The client gets 
            disconnected
            callback(createPlayerObject, null, approve, positionToSpawnAt, Quaternion.identity);
        }

Let me know if you need any extra details.

1 Answers1

0

As said I think you should always spawn the same prefab for all players and rather have a component like e.g.

public class LocalPlayerControl : NetworkBehaviour 
{
    void Start()
    {
        if (IsLocalPlayer) return;

        var cam = GetComponentInChildren<Camera>();
        
        cam.enabled = false;
        // or a bit more radical
        Destroy(cam);
        // or if your cam is on a sub object with additional components (e.g. usually AudioListener etc)
        Destroy(cam.gameObject);

        // same for all other components that you don't want a remote player to have
    }
derHugo
  • 83,094
  • 9
  • 75
  • 115