NetworkView ownership shifts when 2nd player joins, multiple prefab (camera) issues

Hey all,

First multiplayer project, done lots of research to see how everything works, but I can’t seem to figure this out.

For now, the instantiated objects each person controls is a box with a movement script that just pushes it along WASD. The person object also has an attached NetworkView script (set to Unreliable updating since it’s movement).

According to every post I seem to find, just doing something like

if (networkView.isMine)
     Movement();

should work, as it only allows movement per machine if it knows that connected client owns that object.

However, when a second person joins from another compy, it is currently 100% switching them, so they each control one another. I can’t figure out why or what to do against it. I built my simple server hosting bit from the Unity tutorial, so it’s not a master server or anything.

Also, it didn’t talk much about this in the docs: when a new player (camera and all) is instantiated to your local version of the scene, you suddenly have two cameras. What am I supposed to do about that? Delete the components on the other player?

Thanks for any tips, it’s hard to find very specific problems with this.

How are you instantiating the objects ?
For the camera, disable or destroy the ones you don’t need.

I’m using Network.Instantiate(), and do you mean if I’m player 1 and player 2 joins, in player 1’s scene I disable all the camera’s on player 2?

That doesn’t answer my question.
First of all you need to know who is the owner of a networkview. If the server instantiates the objects, networkview.ismine won’t work since the owner will allways be the server.
If each client instantiates it’s own objects then it will and you can use it on each object in the scene to define who controls is.
Being player 1 or 2 or whatever doesn’t matter in multiplayer, it’s server or client.
For the camera’s you will need each client to make sure the correct camera is active and destroy or disable all the other ones.
The key issue is who is the owner ( who instantiates the objects )

if(!networkView.isMine) DestroyCameraOnThisObject();

I’m actively working on this again, so forgive me if I figure it out soon.

I see what you’re saying, and apologize for my lack of an answer. I guess I’m not sure how to differentiate between the server and client doing it when I’m having a client host as well as play. Is it simply on which items the code is attached to?

In my case, I have the code from the unity tutorial that uses simple GUI to connect to or start a server. If I start a server using this item, does that make that GameObject the server “owner”? So I’d have to use Network.Instantiate() with another GameObject to have it treated as a player?

This whole networking business is new and scary to me, and I guess I’m still not 100% on how ownership and such works.

If you’d rather link me to something specific to answer my stupid questions, feel free. :smile:

EDIT: So after testing a bit, is it just separated by IP? So the IP address that calls Network.InitializeServer() is known as the “server”, and anyone who connects to said IP is a “client”? In that case, if I’m setting it up for one player to host and another to join, how do I differentiate them? Just using NetworkPeerType checks?

EDIT2: I tried setting up control using this code:

if (Network.peerType == NetworkPeerType.Server  networkView.owner.ToString () == "0")
	CheckMovement ();
else if (Network.peerType == NetworkPeerType.Client  networkView.isMine)
	CheckMovement ();

and it properly recognized which instance was a client and which was the server, but I still had the issue where when the client joined, they assumed each other’s control. It feels the like the server instance has control as a client as well as a server, and I don’t know how to resolve that. UGH.

EDIT3: Got it working properly, cameras removed and all, for the first time. However, I just want to make sure this is a smart way to do it.

I’m not sure if this is intentional, but using Network.Instantiate() apparently queues up Start() code that is run. Like, when the client joins the server, the server’s Start() code on their already instantiated object runs, and then the client that joined and instantiated has his run. Thanks to that, I just do a check like above, except with failing conditions, wherein I delete all the cameras, which has seemingly worked like a charm.

Is this a proper way to differentiate control over a server and client? This will be a strictly 2-player co-op, so my code is looking like it’ll have to revolve around server player and client player. Sorry for wall of text. :X

There are a few simple ways to differentiate between client and server.
OnServerInitialized and OnPlayerConnected are called on the server.
OnConnectedToServer is called on the client.
If you use a gui button to instantiate your player and you don’t use an RPC to call the instantiate on the server, then the client owns his object.
Assuming you do the last one, you can use :

void Awake()
{
     if (!networkview.ismine)
     {
          enable = false;
     }
}

If the client or server doesn’t own the object, that code will disable the script so you don’t have control over it.
For example a script where you move the player, if it is not disabled, you can control it. Use that code to disable it.

I’m not sure what I was doing to begin with, but the simple networkView.isMine check seems to be working perfectly from server and client end now. I’ve got individual networked control and networked animations working now, so thanks for the help! ^^