Recently, I’ve been trying my hand at basic networking through RPC calls. I’ve made a basic test, where a cube is spawned for each new player that enters, created through the Network.Instantiate function on the player’s connection to the server.
However, when the client does connect, the network view is assigned to the server player. The client’s game state still updates depending on the server player’s input, but has no control over the spawned object.
I’ve had a word with a lecturer about my script, and he sees no issue with it, so I’m thinking it may be an engine setting issue? On the spawned prefab, I’ve set the State Synchronisation to be off, and set observed to none. In the project settings itself, I’ve set the game to be running in the background.
Here’s my code, just in case I’m an idiot and have messed up a line or two.
Network Start (attached to a blank game object)
using UnityEngine;
using System.Collections;
public class NetworkStart : MonoBehaviour
{
public string connectionIP = "127.0.0.1";
public int connectionPort = 25000;
void OnGUI()
{
if (Network.peerType == NetworkPeerType.Disconnected)
{
GUI.Label(new Rect(10, 10, 300, 20), "Status: Disconnected");
if (GUI.Button(new Rect(10, 30, 120, 20), "Client Connect"))
{
Network.Connect(connectionIP, connectionPort);
}
if (GUI.Button(new Rect(10, 50, 120, 20), "Initialize Server"))
{
Network.InitializeServer(4, connectionPort, false);
}
}
else if (Network.peerType == NetworkPeerType.Client)
{
GUI.Label(new Rect(10, 10, 300, 20), "Status: Connected as Client");
if (GUI.Button(new Rect(10, 30, 120, 20), "Disconnect"))
{
Network.Disconnect(200);
}
}
else if (Network.peerType == NetworkPeerType.Server)
{
GUI.Label(new Rect(10, 10, 300, 20), "Status: Connected as Server");
if (GUI.Button(new Rect(10, 30, 120, 20), "Disconnect"))
{
Network.Disconnect(200);
}
}
if(GUI.Button(new Rect(10, 70, 120, 20), "Exit"))
{
Application.LoadLevel("MainMenu");
}
}
}
Spawn Script (also attached to the blank game object)
using UnityEngine;
using System.Collections;
public class NetSpawnSimple : MonoBehaviour
{
public Transform playerPrefab;
private Transform playerCube;
public int playerNum;
void OnServerInitialized()
{
Debug.Log (Network.player+" has joined");
SpawnPlayer(Network.player);
}
void OnPlayerConnected(NetworkPlayer newPlayer)
{
Debug.Log (newPlayer+" has joined");
SpawnPlayer(newPlayer);
}
void OnDisconnectedFromServer(NetworkDisconnection info)
{
if(Network.isServer)
{
Debug.Log ("Player "+playerCube.name+"has left.");
Network.Destroy (playerCube.gameObject);
playerNum--;
}
else
{
if (info == NetworkDisconnection.LostConnection)
{
Debug.Log("Lost connection to the server");
}
else
{
Debug.Log("Successfully diconnected from the server");
}
}
}
void OnPlayerDisconnected(NetworkPlayer player)
{
Debug.Log ("Disconnecting Player "+player);
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
playerNum--;
}
void SpawnPlayer(NetworkPlayer newPlayer)
{
playerCube = (Transform)Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
//NetworkViewID id = Network.AllocateViewID();
//playerCube.networkView.viewID = id;
playerNum++;
//Debug.Log("Spawned "+playerCube+" to "+id);
Debug.Log ("Spawned "+playerCube);
}
}
Player Controller (attached to spawned Prefab)
using UnityEngine;
using System.Collections;
public class SimpleCube : MonoBehaviour
{
public NetworkPlayer theOwner;
private bool bCheckedServer;
private Vector3 lastPosition, lastMovement;
private float minMove = 0.1f;
private int playerNum;
void Awake()
{
if (!networkView.isMine)
{
enabled = false;
}
else
{
Debug.Log ("Spawned Properly");
}
if(Network.isServer)
{
Debug.Log ("Is Server");
}
else if(Network.isClient)
{
Debug.Log ("Is Client");
}
}
void Update()
{
/*if(networkView.owner == NetworkPlayer)
{
//networkView.owner = "1";
}*/
if(!bCheckedServer)
{
if(Network.isServer)
{
Debug.Log ("Is Server's");
//Debug.Log ("Type is "+networkView.owner.GetType());
}
else
{
//networkView.owner =
Debug.Log ("Type is "+networkView.owner.GetType()+", is client = "+Network.isClient);
}
bCheckedServer = true;
}
if(networkView.isMine)
//if(theOwner != null && theOwner == NetworkPlayer.player)
{
//Debug.Log (networkView.owner.GetType());
Vector3 moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
float speed = 5.0f;
lastMovement = speed * moveDir * Time.deltaTime;
transform.Translate(lastMovement);
if(Vector3.Distance(transform.position, lastPosition) > minMove)
{
lastPosition = transform.position;
networkView.RPC("SetPosition", RPCMode.Others, transform.position);
}
}
}
void GetPlayerNum(int pNum)
{
playerNum = pNum;
}
[RPC]
void SetPosition(Vector3 newPos)
{
transform.position = newPos;
}
}
Any help with this would be much appreciated.