I have added basic multiplayer networking where each player can create a server or join others’. Locally, between multiple instances of the game running on my PC, this works fine. However when I host a server on my PC and my friend tries to join - and vice versa - the button which calls the joinServer() function has no effect. Locally what happens is that the buttons disappear and a character is instantiated for the joining player.
See my Network Manager and Player scripts below.
NetworkManager.cs
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour
{
private const string typeName = "VeryUniqueTopDownGame";
private const string gameName = "join Lobby";
private HostData[] hostList;
public GameObject playerPrefab;
private void StartServer()
{
Network.InitializeServer(4, 25565, !Network.HavePublicAddress());
MasterServer.RegisterHost(typeName, gameName);
}
void OnServerInitialized()
{
Debug.Log("Server Initializied");
SpawnPlayer();
}
void OnGUI()
{
if (!Network.isClient && !Network.isServer)
{
if (GUI.Button(new Rect(100, 100, 250, 100), "Start Server"))
StartServer();
if (GUI.Button(new Rect(100, 250, 250, 100), "Refresh Hosts"))
RefreshHostList();
if (hostList != null)
{
for (int i = 0; i < hostList.Length; i++)
{
if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), hostList*.gameName))*
_ JoinServer(hostList*);_
_ }_
_ }_
_ }_
_ }*_
* private void RefreshHostList()*
* {*
* MasterServer.RequestHostList(typeName);*
* }*
* void OnMasterServerEvent(MasterServerEvent msEvent)*
* {*
* if (msEvent == MasterServerEvent.HostListReceived)*
* hostList = MasterServer.PollHostList();*
* }*
* private void JoinServer(HostData hostData)*
* {*
* Network.Connect(hostData);*
* }*
* void OnConnectedToServer()*
* {*
* Debug.Log(“Connected to Server”);*
* SpawnPlayer();*
* }*
* private void SpawnPlayer()*
* {*
* Network.Instantiate(playerPrefab, new Vector3(0f, 0f, 0f), Quaternion.identity, 0);*
* }*
}
----------
## PlayerMobility.cs ##
using UnityEngine;
using System.Collections;
public class PlayerMobility : MonoBehaviour
{
* public float speed;*
* void Start ()*
* {*
* }*
* void Update ()*
* {*
* }*
* void FixedUpdate ()*
* {*
* if(networkView.isMine)*
* {*
* var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);*
* Quaternion rot = Quaternion.LookRotation (transform.position - mousePosition, Vector3.forward);*
* transform.rotation = rot;*
* transform.eulerAngles = new Vector3 (0, 0, transform.eulerAngles.z);*
* rigidbody2D.angularVelocity = 0;*
* //Movement toward/away from the mouse*
* float verticalInput = Input.GetAxis (“Vertical”);*
_ rigidbody2D.AddForce (gameObject.transform.up * speed * verticalInput);_
* //Strafing around the mouse*
* float horizontalInput = Input.GetAxis (“Horizontal”);*
_ rigidbody2D.AddForce (gameObject.transform.right * speed/2 * horizontalInput);_
* }*
* }*
* void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)*
* {*
* Vector3 syncPosition = Vector3.zero;*
* if (stream.isWriting)*
* {*
* syncPosition = rigidbody2D.position;*
* stream.Serialize(ref syncPosition);*
* }*
* else*
* {*
* stream.Serialize(ref syncPosition);*
* rigidbody2D.position = syncPosition;*
* }*
* }*
}
Any ideas as to why this may not be working would be greatly appreciated,
Thanks.