x


[Closed] Multiplayer camera

Hello everyone !

I'm confronted to the famous multiplayer cameras problem (I saw a lot a questions about this, but couldn't solve mine...) for my FPS game.

I want to instantiate a player prefab for each client and the server. I need (locally) to attach the main camera to my player prefab, in a specific place of the model.

Here is the code attached to a gameobject NetworkManager, empty, in my scene :

var spawnPos:Transform[];
var player:GameObject;

private var nbSpawnPos:int = -1;

function Start() {
    nbSpawnPos = spawnPos.Length;
    SpawnPlayer();
}

function SpawnPlayer() {    
    var pos:int = Random.Range(0,nbSpawnPos-1);
    var playerInstance:GameObject = Network.Instantiate(player, spawnPos[pos].position, Quaternion.identity, 0);
    var cameraParent : Transform = playerInstance.transform.Find("Player2/BSF10/v6/v5/v4/v3/v2");
    if (playerInstance.networkView.isMine)
    {
          Camera.main.transform.parent = cameraParent;
    }
}

I have a Network View attached to my player prefab. This is actually not working at all :). My camera stays still and does not follow my character, in debug mode I can clearly see that I go into the "if" loop. Does someone has an idea of the problem ?

more ▼

asked Aug 26 '12 at 10:00 PM

KiraSensei gravatar image

KiraSensei
672 12 24 34

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

The question has been closed Aug 29 '12 at 05:06 PM by KiraSensei for the following reason:

The question is answered, right answer was accepted


2 answers: sort voted first

Attach a network view component to your camera, and then attach the following script to it as well.

function Start (){

    if(networkView.isMine){
       GetComponent(Camera).enabled = true;
    }
    else{
       GetComponent(Camera).enabled = false;
    }
}
more ▼

answered Aug 27 '12 at 05:56 PM

valax gravatar image

valax
55 3 7 9

I added this script to my main camera, and attached this camera to my prefab, so I don't need to move it and change the parent anymore. I still have a big problem :

Failed Network.Instantiate because we are not connected.

And the result is, I have only one character, and moving only from one of the 2 instances, the other instance is "following".

I have no idea why there could be a connection problem.

Aug 27 '12 at 06:41 PM KiraSensei

Forgot to tell that the error is because of line :

var playerInstance:GameObject = Network.Instantiate(player, spawnPos[pos].position, Quaternion.identity, 0);

Aug 27 '12 at 09:51 PM KiraSensei

I forgot to say to attach it to your player camera not your main camera. Sorry for the confusion ;)

Aug 28 '12 at 11:37 AM valax

I think your code above should be in OnNetworkInstantiate() instead of Start(). You'd also probably want to do more than simply disable the camera.

Aug 28 '12 at 03:34 PM SomeGuy22

Hi SomeGuy22, can you be more specific in : "You'd also probably want to do more than simply disable the camera." ? What should I do too ?

Aug 28 '12 at 08:48 PM KiraSensei
(comments are locked)
10|3000 characters needed characters left

You were asking more than just disabling the camera, take a look at this code:

function OnNetworkInstantiate (msg : NetworkMessageInfo) {
    // This is our own player
    connectionGUI = gameObject.FindWithTag("ConnectionGUI");
    if (networkView.isMine)
    {
       GetComponent(DisableComponentOnPause).enabled = true;
       GetComponent(MouseLook).enabled = true;
       GetComponent(FPSInputController).enabled = true;
       GetComponent("NetworkInterpolatedTransform").enabled = false;
       gameObject.layer = 8;

       networkView.RPC("AddPlayer", RPCMode.All);
       isMe = true;
    }
    // This is just some remote controlled player
    else
    {
       GetComponent(DisableComponentOnPause).enabled = false;
       GetComponent(MouseLook).enabled = false;

       name += "Remote";
       GetComponent(FPSInputController).enabled = false;
       GetComponent("NetworkInterpolatedTransform").enabled = true;
       gameObject.layer = 0;

       networkView.RPC("AddPlayerNotMe", RPCMode.All);
       isMe = false;
    }
    }

There's a lot of stuff that needs to be adjusted in order for the game to separate the player from another player. The NetworkInterpolatedTransform script is what keeps the other player's movement shown on your computer.

more ▼

answered Aug 28 '12 at 09:28 PM

SomeGuy22 gravatar image

SomeGuy22
174 4 6 9

I understand. I did what you told me, and this is quite better, thanks ! But I still have some weird things happening : when I instantiate the first character, no problem, when the second one is instantiated, it takes the first one position, and the second is somewhere else (I have a spawn script with random positions).

More problematic : when I move, it moves the other player, and not mine, only mouse look is connected to the good player :(. This is with your if/else code, adapted to my 3D model.

Aug 28 '12 at 10:54 PM KiraSensei

Ok forget my mouse look problem, that was something else and it is corrected, but I still have the mixed controls problem : I tested that if I do :

if ( ! networkView.isMine) // !!! NOT isMine { GetComponent(MouseLook).enabled = true; GetComponent(FPSInputController).enabled = true; } ..... It does not work when I am alone, but it is normal when there are two players :( How can they switch like that ??

EDIT : if I disconnect the second player, the first one goes back to its normal position O.O

Raaaaah I don't understand anything to networking :'(

Aug 28 '12 at 11:31 PM KiraSensei

Haha that's how I felt when I first started. Take a look at this example project, it's how I learned:

http://unity3d.com/support/resources/example-projects/networking-example

Aug 29 '12 at 01:51 AM SomeGuy22

Also the reason your characters are moving is because you a NetworkInterpolatedTransform (found in the example) and it needs to only activate on the player that spawns.

Aug 29 '12 at 01:52 AM SomeGuy22
(comments are locked)
10|3000 characters needed characters left

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:

x2994
x681

asked: Aug 26 '12 at 10:00 PM

Seen: 799 times

Last Updated: Aug 29 '12 at 01:52 AM