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 ?
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.