Hi, the title says it all. I’m working on a online multiplayer game. When I start a server everything is fine, but when I connect as client a new prefab is made and controlable by the client. But the camera snaps to the wrong player. I tried it the camera.enabled=true; in the awake but that isn’t working.
Does anyone have any idea how to fix it?
Here’s my code:
#pragma strict
#pragma implicit
#pragma downcast
public var playerPrefab : Transform;
function OnServerInitialized(){
Spawnplayer();
}
function OnConnectedToServer(){
Spawnplayer();
}
function Spawnplayer(){
var myNewTrans : Transform = Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
if(networkView.isMine){
camera.enabled = true;
}
}
function OnPlayerDisconnected(player: NetworkPlayer) {
Debug.Log("Clean up after player " + player);
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}
function OnDisconnectedFromServer(info : NetworkDisconnection) {
Debug.Log("Clean up a bit after server quit");
Network.RemoveRPCs(Network.player);
Network.DestroyPlayerObjects(Network.player);
/*
* Note that we only remove our own objects, but we cannot remove the other players
* objects since we don't know what they are; we didn't keep track of them.
* In a game you would usually reload the level or load the main menu level anyway ;).
*
* In fact, we could use "Application.LoadLevel(Application.loadedLevel);" here instead to reset the scene.
*/
Application.LoadLevel(Application.loadedLevel);
}
Are the cameras of the prefabs disabled by default? If not, then you will want to change your code to: if(!networkView.isMine){ camera.enabled = false; } This will disable all the cameras that do not belong to the current player.
– ByteSheepThanks for replying! Where should I put this? I tried it under the var myNewTrans : Transform = Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0); but it didn't work
– RosehardtYes you would replace line 22-24 with the code above. Got a couple questions though: - Are the cameras on the player prefab you are instantiating enabled by default? - What is 'camera' referencing? - It should be referencing the camera attached to the prefab that this script is being executed on
– ByteSheepYou would want to add a script to your player prefab that checks which camera should be enabled - something like: var cam : GameObject; function Awake(){ if(!networkView.isMine) { //We aren't the network owner cam.SetActive(false); } } Of course make sure the prefab you add this script to has a network view.. You could also add this script directly to the camera, which would save having to reference it. I'm afraid networking is a large topic - I would suggest going over how network view components work and how to use them in your code.
– ByteSheepThanks for the help so far. I tried this in multiple versions but still nothing. Any other idea's?
– Rosehardt