1
\$\begingroup\$

I created a "network manager" and a "network manager hud" for the whole scene. My character is a simple capsule and has a camera, a weapon spawner and 3 weapons as children. I gave him a "network identity" and a "network transform", rewrote the "capsule controler" (attached to the capsule) and also the "mouse controller" (attached to the camera) and saved it as a prefab. I configured the network manager to take the prefab as spawn object and use the default settings of the network manager: localhost:7777.

Now my problem.

I have build an instance of the game, run the inline game as host (localhost:7777) and everything works. Then i run the external build (tried localhost and even local ip)

As soon as i join the game using the 2. instance, the second player take over the camera of the first player! And in the first instance it seems like both cameras are rendered in paralell.

If i dont attach cameras to the player, i can control them as expected :#

Now, finally my question: What am i doing wrong or do i miss?

Thank you

\$\endgroup\$
0
\$\begingroup\$

You need a way of telling which instance owns which gameobject. Only the instance that "owns" the gameobject should be able to assume control on it.

You are looking for something like this:

public void Update()
{
    if(_thisInstance != _ownerInstance)
        return; // short-circuit if we are not the owner of this gameobject.

    /*
     *   normal control logic goes here
     */
}
\$\endgroup\$
  • \$\begingroup\$ Thank you! I am using if(!isLocalPlayer) return;. Is that what you mean? I have the "camera movement script" attached to the camera, which has no "network identity"!!! But the character movement is attached to the capsule (with network identity). Can this cause the problem? \$\endgroup\$ – MBrain Apr 12 at 15:54
0
\$\begingroup\$

If anybody ran into the same issue, my setup was wrong. Dont attach a camera onto every player prefab. Create them without a camera but put a camera in the scene. Then when a player joins, he take the camera in his local instance of the game and make it a child of himself so it will follow him. This way there is always just a single camera in every instance.

I put a Gameobject (Empty) onto the prefab for that.

Camera.main.transform.position = this.transform.Find("Head").transform.position; 
Camera.main.transform.LookAt(this.transform.position + this.transform.forward * 30);
Camera.main.transform.parent = this.transform;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.