network.instantiate Players

I’m having huge issues with instantiating players (using the first person controller).

When ever a server is initialized or someone connects to the server, the spawnpoint in the level has a script with a function that is called that does this:

function OnNetworkLoadedLevel ()
{

Network.Instantiate(players, transform.position, transform.rotation, 0);

}

With players being the first person controller prefab.

Couple issues with this is that when someone connects, on the server the prefab is instantiated but control and camera view is switched to this prefab and the one instantiated by the server on initialization just sits there.

On the screen of the connecting player, they are just alone as if it were single player.

How do I make it so that the first person controllers instantiated by the server and client are controlled by who spawned them and how do I make it so the client can see the server’s player?

My network script :

var remoteIP = “127.0.0.1”;
var remotePort = 25000;
var listenPort = 25000;

function OnGUI ()
{

if (Network.peerType == NetworkPeerType.Disconnected)
{
	if (GUI.Button (new Rect(10,10,100,30),"Connect"))
		{
			// Connecting to the server
			Network.Connect(remoteIP, remotePort);
		}
		
	if (GUI.Button (new Rect(10,50,100,30),"Start Server"))
		{
			// Creating server
			Network.InitializeServer(32, listenPort, true);
			// Notify our objects that the level and the network is ready
			
			for (var go : GameObject in FindObjectsOfType(GameObject))
				{
				go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
				}
				
				
		}
		
	// Fields to insert ip address and port
	remoteIP = GUI.TextField(new Rect(120,10,100,20), remoteIP);
	remotePort = parseInt (GUI.TextField(new Rect(230,10,40,20), remotePort.ToString()));
	
}

else
{
	
	
	if (GUI.Button (new Rect(10,10,100,50),"Disconnect"))
		{
		// Disconnect from the server
		Network.Disconnect(200);
		}
		
}

}

function OnConnectedToServer ()
{

// Notify our objects that the level and the network are ready
for (var go : GameObject in FindObjectsOfType(GameObject))
	go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);

}

I ran into this issue when I was first starting with networking in Unity, and I still haven’t quite figured out player creation yet. But imagine it this way: With that function you are sending a message to all computers on the network to instantiate that prefab, which includes a brand new camera with it. Check out your scene view after a few people have connected; Cameras, cameras everywhere!

This is not only unnecessary because one client doesn’t need a camera for other players’ view but there is also the possibility that the new camera will render “on top” of your other camera.

Another flaw with the first person character controller is that it derives it’s movements from the local Input class and thus everyone seems to copy your movements. What you need is a way to disable player control on characters that don’t belong to the client.

Although I cannot reasonably answer your question on how to fix these issues I can link you over to the Unity Muliplayer Tutorial that started me on my quest. If you’re not using an iPhone you may find this thread will save you a bit of hardship.

Hope this is helpful and good luck on your journey to muliplayerdom,

it’s a hard one :wink:

I ran into this issue also - you basically have to just have one camera in the scene, the main camera. And then set your players to use the main camera when they instantiate. See this post for more details.

I prefer to have a local object, some sort of controller that owns and manipulates the camera (and any other clientside stuff like GUI) and has a reference to the controlled player. Then you can instantiate as many players as you wish and just tell the controller which one to control.

So, you got two options:

  1. The controller tells the player that it’s locally controlled, then player can read input and do movements
  2. (I prefer this one, more clear, flexible and reusable for different scenarios) Player doesn’t read input itself, but the controller does and then it calls appropriate methods on its selected player.

Let’s say that your player entity knows how to reach certain point in world (like in a point and click game). So if you call the player’s GoTo(Vector3) method, it will walk there, including pathfinding, obstacle avoiding, energy spend and so on. And you can tell it to stop and cancel any action by calling StopAll(). You can call these methods both by your player controller or by some AI component, networking…
Then you have the controller which does fancy stuff like GUI, camera / map scrolling, showing name above the controlled player’s name. And you can simply attach any player to it. If you want splitscreen, you just duplicate the controller, assign it different key bindings and link it to a different player (probably also set the multiple cameras to render properly on split screen). The controller will then call proper actions on the player if you click your mouse or press your key.

To work with networking, server simply tells the client which player is his local to control, then client assigns this player to his local controller. Bam, easy. Don’t forget to add some anti-hack check (clients can’t send control messages for players they are not supposed to control, etc)