[FIX: Uncheck Advanced Configuration]
I have created a simple networking test with the help of some tutorials. The player is just a square that moves to a random position whenever you press Spacebar. The problem is, that if i use SyncVars, or ClientRpc tagged functions, the client will crash at random, sometimes the seccond they join, and sometimes a minute later. If i comment out the line where RpcClientMovement is called, inside the CmdServerMovement function below, it never times out. Is there something i’m missing, or a workarround to the problem? I tried setting the Network manager times to very high and very low values, without success.
My player has a Network Identity, with Server Only unchecked, and Local player authority checked.
Here’s my code:
Network manager script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Networking;
public class NetworkManager_Custom : NetworkManager {
void Start(){
SetupMenuSceneButtons();
}
public void StartupHost(){
SetPort();
NetworkManager.singleton.StartHost();
}
public void JoinGame(){
SetIPAdress();
SetPort ();
NetworkManager.singleton.StartClient();
}
void SetIPAdress(){
string ip = GameObject.Find("Join IP InputField").transform.FindChild("Text").GetComponent<Text>().text;
if (ip == "") {
ip = "localhost";
}
NetworkManager.singleton.networkAddress = ip;
}
void SetPort(){
NetworkManager.singleton.networkPort = 7777;
}
void SetupMenuSceneButtons() {
//GameObject.Find ("Host Button").GetComponent<Button>().onClick.RemoveAllListeners();
//GameObject.Find ("Join Button").GetComponent<Button>().onClick.RemoveAllListeners();
GameObject.Find ("Host Button").GetComponent<Button>().onClick.AddListener(StartupHost);
GameObject.Find ("Join Button").GetComponent<Button>().onClick.AddListener(JoinGame);
}
void SetupGameSceneButtons() {
GameObject.Find ("Disconnect Button").GetComponent<Button>().onClick.AddListener(NetworkManager.singleton.StopHost);
}
//Como el NetworkManager se mueve de scene en scene, se pierden las referencias de insepector al Network manager de los otros objetos.
//Tengo que hacer los vinculos por codigo.
void OnLevelWasLoaded (int level) {
if (level == 0) {
SetupMenuSceneButtons();
} else {
SetupGameSceneButtons();
}
}
}
Player script:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.UI;
public class NewPlayerScript : NetworkBehaviour {
public Vector2 targetPos = Vector2.zero;
float speed = 0.5f;
void Start () {
if (isLocalPlayer) {
CmdSpawn(new Vector2(Random.Range(-3f,3f), Random.Range(-3f,3f)));
}
}
void Update () {
if (!isLocalPlayer)
return;
if (Input.GetKeyDown(KeyCode.Space)) {
targetPos = new Vector2(Random.Range(-3f,3f), Random.Range(-3f,3f));
CmdServerMovement(targetPos);
}
}
void FixedUpdate () {
Vector2 position = new Vector2(this.transform.position.x, this.transform.position.y);
if ((targetPos-position).magnitude < speed) {
this.transform.position = targetPos;
} else {
Vector2 movement = (targetPos-position).normalized*speed;
this.transform.Translate(movement);
}
}
[Command]
public void CmdServerMovement(Vector2 newPos){
targetPos = newPos;
RpcClientMovement(newPos);
}
[ClientRpc]
public void RpcClientMovement(Vector2 newPos) {
if (!isLocalPlayer) {
targetPos = newPos;
}
}
[Command]
public void CmdSpawn(Vector2 newPos) {
this.transform.position = new Vector2(newPos.x, newPos.y);
targetPos = new Vector2(this.transform.position.x, this.transform.position.y);
}
}
Network manager inspector configuration:
Thanks in advance for your response!
