x


Networking Script Controls the Opposite Player

I have made two netowrking scripts so far. here is the first.

using UnityEngine;
using System.Collections;
using System;

public class MPBase : MonoBehaviour 
{

    public string connectToIp = "127.0.0.1";
    public int connectPort = 25000;
    public bool useNAT = false;
    public string ipaddress = "";
    public string port = "";

    string playerName = "<NAME ME>";

    void OnGUI()
    {
        if (Network.peerType == NetworkPeerType.Disconnected)
        {
            if (GUILayout.Button("Connect"))
            {
                if (playerName != "<NAME ME>")
                {
                    Network.useNat = useNAT;
                    Network.Connect(connectToIp, connectPort);
                    PlayerPrefs.SetString("playerName", playerName);
                }
            }

        if (GUILayout.Button("Start Server"))
        {
            if (playerName != "<NAME ME>")
            {
                Network.useNat = useNAT;
                Network.InitializeServer(32, connectPort);

                foreach (GameObject go in FindObjectsOfType(typeof(GameObject)))
                {
                    go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
                }
                PlayerPrefs.SetString("playerName", playerName);
            }
        }
            playerName = GUILayout.TextField(playerName);
            connectToIp = GUILayout.TextField(connectToIp);
            connectPort = Convert.ToInt32(GUILayout.TextField(connectPort.ToString()));
        }
        else
        {
            if (Network.peerType == NetworkPeerType.Connecting) GUILayout.Label("Connect Status : Connecting");
            else if (Network.peerType == NetworkPeerType.Client)
            {
                GUILayout.Label("Connection Status : Client!");
                GUILayout.Label("Ping to Server: " + Network.GetAveragePing(Network.connections[0]));
            }

            else if (Network.peerType == NetworkPeerType.Server)
            {
                GUILayout.Label("Connection Status : Server!");
                GUILayout.Label("Connections: " + Network.connections.Length);
                if (Network.connections.Length >= 1)
                    GUILayout.Label("Ping to Server: " + Network.GetAveragePing(Network.connections[0]));
            }

            if (GUILayout.Button("Disconnect"))
                Network.Disconnect(200);

            ipaddress = Network.player.ipAddress;
            port = Network.player.port.ToString();
            GUILayout.Label("IP Address: " + ipaddress + ":" + port);
        }
    }

    void OnConnectedToServer()
    {
        foreach (GameObject go in FindObjectsOfType(typeof(GameObject)))
        {
            go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
        }
    }
}

This was written in C#. Now here is the next, also written in C#

using UnityEngine;
using System.Collections;

public class SpawnScript : MonoBehaviour 
{
    public Transform player;

    void OnServerInitialized()
    {
        SpawnPlayer();
    }

    void OnConnectedToServer()
    {
        SpawnPlayer();
    }

    void SpawnPlayer()
    {
        Network.Instantiate(player, transform.position, transform.rotation, 0);
    }

    void OnPlayerDisconnected(NetworkPlayer player)
    {
        Network.RemoveRPCs(player);
        Network.DestroyPlayerObjects(player);
    }

    void OnDisconnectedFromServer(NetworkDisconnection info)
    {
        Network.RemoveRPCs(Network.player);
        Network.DestroyPlayerObjects(Network.player);
        Application.LoadLevel(Application.loadedLevel);
    }
}

And I also added this code to my MouseLook script and FPS Script (JavaScript)

function Start()
{
    if(!networkView.isMine)
    {
        enabled = false;
    }
}

function Whatever() {
if(networkView.isMine){
//do whatever 
}
}

The only problem is that when I connect with two clients, Player1 controls player 2's character and Player2 controls Player1's. Please leave a response or help me one on one by contacting me at fiercemodz@gmail.com

more ▼

asked Jan 29 '11 at 01:59 AM

Fierce Waffle gravatar image

Fierce Waffle
176 34 38 49

I've the same problem, can you tell me how to fix it?

Jul 25 '11 at 03:56 PM marf
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Your need is documented in the official netwokring example. What you basically need is to get the 'others' controller scripts and disable them in each client, once they spawn. However this is only a small part of the whole solution, which is pretty complicated. link text

more ▼

answered Aug 05 '11 at 10:33 AM

roamcel gravatar image

roamcel
1.2k 37 40 44

(comments are locked)
10|3000 characters needed characters left

Hey, I'm not sure if you have solved this yet or not, but I had a similar problem. If yours is what mine was, all you'll need is to create a .js file and put it on the camera. This is all you need in that file. function Awake() {

if(networkView.isMine){
    camera.enabled = true;
}else{
    camera.enabled = false;
}

}

more ▼

answered Dec 29 '11 at 04:28 AM

suyujin gravatar image

suyujin
71 2 2 5

(comments are locked)
10|3000 characters needed characters left

Hey. Check if setting "observed" field in your "network view" component to none (instead of a supposed Transform:GameObject that might be there, would help. there's a possibility that you want to use the networkView just for RPC calls and, by mistake, you're updating everyone's position this way.

At least that's what happened to me before.

more ▼

answered Jan 29 '11 at 12:42 PM

seth gravatar image

seth
21 3 6 10

So, i need to change the observed to none?

Jul 25 '11 at 03:56 PM marf
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1172
x1044
x709
x141
x45

asked: Jan 29 '11 at 01:59 AM

Seen: 3064 times

Last Updated: Dec 29 '11 at 04:29 AM