4

I have been working in unity for a little while now, and have gotten a basic multiplayer setup to work, from following this tutorial. However, I want to add a couple more things, one of them being a camera to follow each player. I have looked this up already and even found a comment from the creator of that tutorial. So I followed what he explained, and have this code:

function spawnPlayer(){
    var player:GameObject=Network.Instantiate(playerPrefab,spawnObject.position,Quaternion.identity,0);
    Destroy(Camera.main);
    if(player.networkView.isMine){
       var playercam:Transform=player.transform.find("Camera");
       playercam.tag="MainCamera";
    }
}

I forgot to mention, there is a camera parented to the player object, which is selected as the 'playerPrefab' game object.

This kind of works, but not really. When I start a server, the camera follows you fine. However when I open another file and join that host, it starts getting all weird on me. By weird, I mean that when i move on the server game, the camera follows the server player on the client game, and when moving on the client game, the camera follows the client on the server game. To me this makes no sense.

I'd appreciate anyone's input on this problem, or how it can be fixed. Thanks

Steven
  • 166,672
  • 24
  • 332
  • 435
Jeremy Darrach
  • 273
  • 1
  • 6
  • 18
  • You want to make only one camera, don't include a camera with the player prefab. I think you're creating too many cameras. – mash Jun 17 '13 at 15:00
  • So if I dont parent a camera to the player, what do I do with the main camera to make it follow the player – Jeremy Darrach Jun 17 '13 at 15:05
  • Attach a script which forces the camera to follow the correct player object. – mash Jun 17 '13 at 19:10
  • yea i was thinking about that, like for each client, that camera would follow the last player to join... However i don't know how to get the number of instances a certain object, and pick a specific one. – Jeremy Darrach Jun 17 '13 at 23:55
  • @JeremyDarrach I think there is a disconnect here. Who calls `SpawnPlayer`, the server or the client? If client, you can skip the isMine thng and just get the main camera. If it's the server, then you need to move everything below Network.Instantiate to [MonoBehaviour.OnNetworkIsInstantiate](http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnNetworkInstantiate.html) on a script attached to your player prefab. – Jerdak Jun 18 '13 at 01:10

1 Answers1

2

Your code is incomplete: as you have a camera in the prefab itself you have to continue the if(player.networkView.isMine) adding an else clause to destroy the camera in case you are just spawning the image of a remote player.

ronalchn
  • 12,225
  • 10
  • 51
  • 61