OK, I’ve got the network connect working, but without a simple networking tutorial that works (Could be my network…) I’m finding it a little hard to work out how to get people moving around.
I can use Network.Instantiate to clone a prefab in the world.
But I’m not sure how to go about creating new objects from prefabs to indicate other players inworld, which instantiate where that player is when they connect to the server, then move as other players move.
Thanks to AngryAnt on IRC I know I need to use OnSerializeNewtorkView, which I think needs to be applied to the object I am cloning (that indicates other players, for this I’m just going to use a textured cube for now).
Anything along the line of… use this class/function, here, here, here would help.
So far on my little script I’ve gotten…(And I know I haven’t dealt with disconnections yet…)
var textFieldString = "App Started";
var scrollViewVector: Vector2 = Vector2.zero;
var playerCount: int = 0;
var typingArea = "";
var playerPrefab : Transform;
function OnGUI () {
scrollViewVector = GUI.BeginScrollView(Rect(25,25,220,200), scrollViewVector,Rect(0,0,180,2000));
textFieldString = GUI.TextArea(Rect(0,0,200,2000 ),textFieldString);
GUI.EndScrollView();
typingArea = GUI.TextArea(Rect(25,225,200,100),typingArea);
if(GUI.Button(Rect (225,225,100,100),"Send")){
SendText();
}
if(GUI.Button(Rect (425,0,150,50),"Launch Server")){
LaunchServer();
}
if(GUI.Button(Rect (425,50,150,50),"Connect")){
ConnectToServer();
}
}
function LaunchServer(){
textFieldString = textFieldString + "\nCreating Server";
Network.incomingPassword = "";
Network.InitializeServer(32,25000);
}
function OnServerInitialized(){
Debug.Log("Server initialized and ready");
textFieldString = textFieldString + "\nServer Created";
}
function ConnectToServer(){
textFieldString = textFieldString + "\nConnecting to Server";
Network.Connect("147.197.166.113",25000,"");
}
function OnConnectedToServer(){
textFieldString = textFieldString + "\nConnected To Server";
Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
}
function OnPlayerConnected(player: NetworkPlayer){
textFieldString = textFieldString + "\nPlayer" + playerCount++ + "connected from " + player.ipAddress + ":" + player.port;
}
function SendText(){
//@RPC
textFieldString = textFieldString + "\nSending Text";
networkView.RPC ("PrintText", RPCMode.All, typingArea);
typingArea ="";
}
@RPC
function PrintText (text : String, info : NetworkMessageInfo)
{
textFieldString = textFieldString + "\n" + info.sender + " says:" + text;
}