Like the title says, I just added a Mp mode to my game. It works, but the only thing tis that levels are randomly generated so now each player sees a different world. Here’s my generation script:
using System.Collections;
public class LevelGenMP : MonoBehaviour {
public int MaxObjects;
public int MaxRange;
public GameObject[] Objects;
public Vector3[] VObjects;
// Use this for initialization
void Start () {
}
void StartingServer(){
if(Network.isServer){
if(PlayerPrefs.GetString("World Size") == "Small"){
for(int i = 0; i < 35; i++){
//Instantiate(Objects[Random.Range(0, Objects.Length - 1)], new Vector3(Random.Range(-MaxRange, MaxRange), this.transform.position.y, Random.Range(-MaxRange, MaxRange)), Quaternion.identity);
GameObject newThing = (GameObject) GameObject.Instantiate(Objects[Random.Range(0, Objects.Length - 1)], new Vector3(Random.Range(-150, 150), this.transform.position.y, Random.Range(-MaxRange, MaxRange)), Quaternion.identity);
newThing.transform.parent = this.transform;
VObjects *= newThing.transform.position;*
-
}* -
}* -
if(PlayerPrefs.GetString("World Size") == "Medium"){* -
for(int i = 0; i < 70; i++){* -
//Instantiate(Objects[Random.Range(0, Objects.Length - 1)], new Vector3(Random.Range(-MaxRange, MaxRange), this.transform.position.y, Random.Range(-MaxRange, MaxRange)), Quaternion.identity);* -
GameObject newThing = (GameObject) GameObject.Instantiate(Objects[Random.Range(0, Objects.Length - 1)], new Vector3(Random.Range(-250, 250), this.transform.position.y, Random.Range(-MaxRange, MaxRange)), Quaternion.identity);* -
newThing.transform.parent = this.transform;*
_ VObjects = newThing.transform.position;_
* }*
* }*
* if(PlayerPrefs.GetString(“World Size”) == “Large”){*
* for(int i = 0; i < 70; i++){*
* //Instantiate(Objects[Random.Range(0, Objects.Length - 1)], new Vector3(Random.Range(-MaxRange, MaxRange), this.transform.position.y, Random.Range(-MaxRange, MaxRange)), Quaternion.identity);*
* GameObject newThing = (GameObject) GameObject.Instantiate(Objects[Random.Range(0, Objects.Length - 1)], new Vector3(Random.Range(-250, 250), this.transform.position.y, Random.Range(-MaxRange, MaxRange)), Quaternion.identity);*
* newThing.transform.parent = this.transform;*
_ VObjects = newThing.transform.position;
* }
}else{
for(int i = 0; i < 35; i++){
//Instantiate(Objects[Random.Range(0, Objects.Length - 1)], new Vector3(Random.Range(-MaxRange, MaxRange), this.transform.position.y, Random.Range(-MaxRange, MaxRange)), Quaternion.identity);
GameObject newThing = (GameObject) GameObject.Instantiate(Objects[Random.Range(0, Objects.Length - 1)], new Vector3(Random.Range(-150, 150), this.transform.position.y, Random.Range(-MaxRange, MaxRange)), Quaternion.identity);
newThing.transform.parent = this.transform;
VObjects = newThing.transform.position;
}
}
}*_
* if(Network.isClient){*
* SetLevelFromMaster(VObjects);*
* }*
* }*
[RPC]
void SetLevelFromMaster(Vector3[] Positions){
* for(int i = 0; i < 35; i++){*
GameObject newThing = (GameObject) GameObject.Instantiate(Objects[Random.Range(0, Objects.Length - 1)], new Vector3(VObjects.x, VObjects_.y, VObjects*.z), Quaternion.identity);
newThing.transform.parent = this.transform;
}
}*_
}
Here’s the connection code(direct connect code)
using UnityEngine;
using System.Collections;
public class ConnectionGUI : MonoBehaviour {
public string remoteIP= “127.0f.0.1f”;
public int remotePort= 25000;
public int listenPort= 25000;
public bool useNAT= true;
public string yourIP= “”;
public string yourPort= “”;
public GameObject Level;
void Awake (){
Level = GameObject.Find(“level”);
if (FindObjectOfType(typeof(MasterServerGUI))){
this.enabled = false;
}
if(FindObjectOfType(typeof(UDPConnectionGUI)))
this.enabled = false;
}
void OnGUI (){
if (Network.peerType == NetworkPeerType.Disconnected){
// If not connected
if (GUI.Button (new Rect(10,10,100,30),“Connect”))
{
// Connecting to the server
Network.Connect(remoteIP, remotePort);
Level.SendMessage(“StartingServer”);
Debug.Log("Server = " + Network.isServer + " Client = " + Network.isClient);
}
if (GUI.Button (new Rect(10,50,100,30),“Start Server”)){
// Creating server
Network.InitializeServer(8 ,listenPort, false);
// Notify our objects that the level and the network is ready
foreach (GameObject go in FindObjectsOfType(typeof(GameObject))){
go.SendMessage(“OnNetworkLoadedLevel”, SendMessageOptions.DontRequireReceiver);
}
* Level.SendMessage(“StartingServer”);*
* Debug.Log("Server = " + Network.isServer + " Client = " + Network.isClient);*
}
remoteIP = GUI.TextField(new Rect(120,10,100,20),remoteIP);
remotePort = int.Parse(GUI.TextField(new Rect(230,10,40,20),remotePort.ToString()));
}else{
// If connected
// Getting your ip address and port
yourIP = Network.player.ipAddress;
yourPort = Network.player.port.ToString();
GUI.Label(new Rect(140,20,250,40),“IP Adress: “+yourIP+”:”+yourPort);
if (GUI.Button (new Rect(10,10,100,50),“Disconnect”)){
// Disconnect from the server
Network.Disconnect(200);
}
}
}
void OnConnectedToServer (){
// Notify our objects that the level and the network is ready
foreach (GameObject go in FindObjectsOfType(typeof(GameObject)))
go.SendMessage(“OnNetworkLoadedLevel”,
SendMessageOptions.DontRequireReceiver);
}
void OnDisconnectedFromServer (){
if (this.enabled != false)
Application.LoadLevel(Application.loadedLevel);
else{
//NetworkLevelLoad _NetworkLevelLoad = FindObjectOfType(typeof(NetworkLevelLoad));
NetworkLevelLoad _NetworkLevelLoad = FindObjectOfType(typeof(NetworkLevelLoad)) as NetworkLevelLoad;
_NetworkLevelLoad.OnDisconnectedFromServer();
}
}
}
and here’s the game:
_*https://dl.dropbox.com/u/85846965/OGAM/january/Webplayer/Webplayer.html*_
Okay. Now, did you try copying the generated level from the host to the clients?
– LoiusHow would I do that? Sorry, just getting into networking.
– chrisall76