- Home /
Camera switches player in network game
Ok so what is happening is my camera is switching between players so the other player looks though my screen/camera and i look though his camera.
how i set it up is that one player is the batmobile and the other is a race car. they are both prefabs with its own camera attached to it and movement script.
when I start the server on my end before anyone connects then it works fine im behind the batmobile. But the second he joins the game i switch behind the race car and his camera is behind the batmobile.
The movement script still works fine I control the batmobile and he controls the race car but the cameras are screwy.
Thanks for any help with this Im stumped about this.
here is the network scripts Thanks again for any help
var playerPrefab : GameObject;
var batPrefab : GameObject;
var sp1 : Transform;
var sp2: Transform;
private var hostData: HostData[];
private var refreshing : boolean;
var bX : float;
var bY : float;
var bW : float;
var bH : float;
function Start()
{
bX = Screen.width * 0.1;
bY = Screen.width * 0.1;
bW = Screen.width * 0.1;
bH = Screen.width * 0.1;
}
function Update()
{
if(refreshing)
{
if(MasterServer.PollHostList().Length > 0)
{
refreshing = false;
Debug.Log(MasterServer.PollHostList().Length);
hostData = MasterServer.PollHostList();
Debug.Log("Host data lenght ;" + hostData.Length);
}
}
}
function startServer()
{
Network.InitializeServer(32,25000,!Network.MovePublicAddress);
MasterServer.RegisterHost(gameName, "Batman Driver game", "This is a test");
}
function OnServerInitialized()
{
Debug.Log("Server initilized");
spawnPlayer();
}
function OnMasterServerEvent(mse:MasterServerEvent)
{
if(mse == MasterServerEvent.RegistrationSucceeded)
{
Debug.Log("Server Registered");
}
}
function refreshHostList()
{
Debug.Log("Refreshing");
MasterServer.RequestHostList(gameName);
refreshing = true;
}
function OnConnectedToServer()
{
spawnPlayer();
}
function spawnPlayer()
{
if(Network.isClient)
{
Network.Instantiate(playerPrefab, sp2.position, Quaternion.identity, 0);
}
if(Network.isServer)
{
Network.Instantiate(batPrefab, sp1.position, Quaternion.identity, 0);
}
}
function OnGUI()
{
if(!Network.isClient && !Network.isServer)
{
if(GUI.Button(Rect(bX, bY, bW, bH), "Start Server"))
{
Debug.Log("Server Start");
startServer();
}
if(GUI.Button(Rect(bX, bY * 1.2 + bH, bW, bH), "Refreash Host"))
{
refreshHostList();
}
//this works
//GUI.Button(Rect(bX * 1.5 + bW, bY * 1.2 + (bH * 0), bW *3 , bH * 0.5), "Game test");
if(hostData)//if statement works
{
var hdLenght = hostData.Length;
//GUI.Button(Rect(bX * 1.5 + bW, bY * 1.2 + (bH * 0), bW *3 , bH * 0.5), "Game test");\
for(var i: int = 0; i < hdLenght; i++)
{
Debug.Log("Game name :" + hostData[0].gameName);
if(GUI.Button(Rect(bX * 1.5 + bW, bY * 1.2 + (bH * i), bW *3 , bH * 0.5), hostData[i].gameName))
{
Debug.Log("Connecting to game");
Network.Connect(hostData[i]);
}
}
}
}
}
Answer by Jake.OConnor · Dec 13, 2012 at 07:34 PM
Well nothing in the code you posted has anything to do with a camera, but I am led to believe that your cameras are probably children of the vehicle prefabs, which would mean that when a new vehicle is instantiated, there are two cameras in the scene rendering to the same screen space. Remove the cameras from the vehicle prefabs and instead have a single camera in the scene that targets and follows the local player.
Yes that's right each prefab has its own camera. I wanted to do that because i have to place the camera in a certain position on the model. It is very hard to do that in code
The camera is used like a fps camera so its from the players perspective.
Any suggestions?
Pretty easy. $$anonymous$$ake a new dummy GameObject as a child of each model, then place them at the same position as the cameras. Delete the cameras and just leave one in the scene. Once a vehicle is instantiated for the local player, have the main camera's position and rotation be set to that of the dummy object and parent the camera to the vehicle so that it follows it.
brilliant I was hitting my head against the wall to figure out how to get the can attached to the model thank a lot!
No problem. I had difficulties like these when I was making my multiplayer game a few months back, so I learned the hard way.