looking for help on a simple network demo

in my quest to understand the magic of networking, i’ve decided to try and build a simple network demo of my own. what im trying to do is essentially remake the third person network demo, sans Lerpz. just objects moving around in the same world. right now, i dont even need the ability to search for servers, just typing in an ip and port is good.

what ive got now are buttons that let me create a server or connect to a specific ip. they appear to work fine. however, when a client connects and spawns a player, there are obviously major issues with deciding which machine controls which player. it appears that everyone connected, the server included, trys to control every player in the game. when the first client spawns, the cameras in the each player switch, so that the host is looking through the newly created player’s camera and the new client is looking through the host’s original camera.

ive obviously missed a step (or twelve) somewhere, but for the life of me i cant figure it out. i’d greatly appreciate it if someone could tell me the dumb mistake i made or the flaw in my understanding of how this all works.

heres the code im using:

var IP = "000.000.00.0";
var Port = "25000";

function OnGUI () {
	
	IPa = GUI.TextField (Rect (Screen.width - 90, 80, 85, 20), IP);
	Port = GUI.TextField (Rect (Screen.width - 90, 105, 50, 20), Port);
	
	if (GUI.Button (Rect (Screen.width - 90, 20,70,25), "Host")) {
		Network.InitializeServer(10, 25000); // allow 10 players, port 25000
		
		
	}
	
	if (GUI.Button (Rect (Screen.width - 90, 50,70,25), "Connect")) {
		var Portint = parseInt(Port);
		Network.Connect(IP, Portint);
		
	}
	
}
// remove gui once connected/created server
function OnConnectedToServer() {
	Destroy(this);
}


function OnServerInitialized()  {
	Destroy(this);
	
}

///////// separate script file

var playerTube : Transform;



function OnConnectedToServer() {
	Network.Instantiate(playerTube, transform.position, transform.rotation, 0);
	Debug.Log("connected to server");
	
}


function OnServerInitialized()  {

	Network.Instantiate(playerTube, transform.position, transform.rotation, 0);
	Debug.Log("server made");
	
}

all this code is attached to an empty game object, and my player prefab has a network view with state synchronization set to reliable and observing the player prefab’s transform.

thanks a lot for reading,
jason

Looking at the code, the objects seen to instantiate fine.

What you’ve probably missed is that both objects, regardless of who network.instantiated them will have the input (camera) controll scripts. Unity will not take care of this, which isn’t strange once you get it.

Add a script to these objects along the lines of:

function OnNetworkInstantiate(){
  if(networkView.isMine){
     //enable local-user control
     //disable some non-localplayer stuff
  }else{
     //disable local-user control
     //This is an external player
  }
}

thanks for the reply! sorry i took so long to respond, i was on vacation.

that made it work. it does make sense once you see all the pieces working together.

the scripts are attached, if anyone wants to take a look.

thanks again,
jason

112409–4319–$netwrok_scripts_130.js (328 Bytes)
112409–4320–$gui_elements_563.js (676 Bytes)
112409–4321–$addcomps_149.js (173 Bytes)