Muted Games
Muted Games

Reputation: 9

Networking Problems - Server controls client

I'm trying to develop an online game where people can just hang out and chat, but for some reason the client's cube doesn't show up on the server's game and vice versa, but the server controls the client and the server. Here is the code for both files:

Networking Script:

using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour
{

string registeredGameName = "Hangout Zone";
bool isRefreshing = false;
float refreshRequestLength = 3.0f;
HostData[] hostData;

private void StartServer()
{
    Network.InitializeServer(16, 25002, false);
    MasterServer.RegisterHost(registeredGameName, "Hangout Zone", "The place to chill with friends.");
}

void OnServerInitialized()
{
    Debug.Log("Server has been initialized.");
}

void OnPlayerDisconnected(NetworkPlayer player)
{
    Debug.Log("Player disconnected.");
    Network.RemoveRPCs(player);
    Network.DestroyPlayerObjects(player);
}

void OnApplicationQuit()
{
    if (Network.isServer)
    {
        Network.Disconnect(200);
        MasterServer.UnregisterHost();
    }

    if (Network.isClient)
    {
        Network.Disconnect(200);
    }
}

void OnMasterServerEvent(MasterServerEvent masterServerEvent)
{
    if (masterServerEvent == MasterServerEvent.RegistrationSucceeded)
    {
        Debug.Log("Registration Successful.");
    }
}

public IEnumerator RefreshHostList()
{
    Debug.Log("Refreshing...");
    MasterServer.RequestHostList(registeredGameName);
    float timeStarted = Time.time;
    float timeEnd = Time.time + refreshRequestLength;

    while (Time.time < timeEnd)
    {
        hostData = MasterServer.PollHostList();
        yield return new WaitForEndOfFrame();
    }

    if (hostData == null || hostData.Length == 0)
    {
        Debug.Log("No active servers have been found.");
    }
    else
    {
        Debug.Log(hostData.Length + " have been found.");
    }

}

private void SpawnPlayer()
{
    Debug.Log("Spawning player...");
    Network.Instantiate(Resources.Load("Prefabs/Block"), new Vector3(0f, 2.5f, 0f), Quaternion.identity, 0);
}



public void OnGUI()
{

    if (Network.isServer)
    {
        GUILayout.Label("Running as a server.");
    }
    else if (Network.isClient)
    {
        GUILayout.Label("Running as a client.");
    }

    if (Network.isClient)
    {
        if (GUI.Button(new Rect(25f, 25f, 150f, 30f), "Spawn"))
        {
            SpawnPlayer();
        }
    }
    if (Network.isClient || Network.isServer)
    {
        return;
    }

    if (GUI.Button(new Rect(25f, 25f, 150f, 30f), "Start New Server"))
    {
        StartServer();
    }
    if (GUI.Button(new Rect(25f, 65f, 150f, 30f), "Refresh Server List"))
    {
        StartCoroutine("RefreshHostList");
    }
    if (hostData != null)
    {
        for (int i = 0; i < hostData.Length; i++)
        {
            if (GUI.Button(new Rect(Screen.width / 2, 65f + (30f * i), 300f, 30f), hostData[i].gameName))
            {
                Network.Connect(hostData[i]);
            }
        }
    }
}
}

Control Script: (This is JavaScript)

#pragma strict

function Update () {
    if (Input.GetKey(KeyCode.W))
    {
        transform.position.z += 0.03;
    }

    if (Input.GetKey(KeyCode.S))
    {
        transform.position.z -= 0.03;
    }

    if (Input.GetKey(KeyCode.A))
    {
        transform.position.x -= 0.03;
    }

    if (Input.GetKey(KeyCode.D))
    {
        transform.position.x += 0.03;
    }
}

Upvotes: 1

Views: 398

Answers (1)

arsenic87
arsenic87

Reputation: 51

Are you missing a OnPlayerConnect or something like that? I only see SpawnPlayer(), and can't see it getting called from anywhere except the button.

Anyway, you are spawning a object with a local control. This means if the second player actually DID spawn more cubes, you would control all of them from all machines. The control script needs to only respond if if sees that the local player is the owner of the object (since i suspect you have the script attached to all blocks spawned).

I think unity has this in the NetworkView.isMine property of the networkview. Add something like this to your controller script:

function Update () {
if (!networkView.isMine) { return; }
if (Input.GetKey(KeyCode.W))
{
    transform.position.z += 0.03;
}

Upvotes: 0

Related Questions