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