- Home /
Camera attached to the wrong player
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.is$$anonymous$$ine){
camera.enabled = false;
}
This will disable all the cameras that do not belong to the current player.
Thanks 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
Yes 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
You were right, I hadn't attatached the prefab. I have now, code looks like this now. But the problem is still there. Did I miss anything? Thanks for your help I have it running on www.adoets.nl if you want to take a look
public var playerPrefab : Transform;
public var cameraPrefab : Camera;
function OnServerInitialized(){
Spawnplayer();
}
function OnConnectedToServer(){
Spawnplayer();
}
function Spawnplayer(){
var myNewTrans : Transform = Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
if(!networkView.is$$anonymous$$ine){
cameraPrefab.enabled=false;
}
}
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);
Application.LoadLevel(Application.loadedLevel);
}
You 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.is$$anonymous$$ine)
{
//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.
Answer by ByteSheep · May 12, 2013 at 06:02 PM
Ok, I decided to make a quick example project showing you how the basic setup works - here it is.
THAN$$anonymous$$ YOU! Oh my god, I could kiss you. It works now!
If this has answered your question then please click the green tick beside the answer to mark it as accepted. Cheers!
Your answer
Follow this Question
Related Questions
Cinemachine Input Provider requires InputActionReference. Any workarounds? 0 Answers
c# set main camera to player (multiplayer) 1 Answer
camera doesnt follow player after I added networkIdentiy and networkTransform to the player 0 Answers
Multiplayer| Attaching camera to player (if i am the owner 1 Answer
How can I get "!networkView.isMine" to function properly? 1 Answer