我有这个网络脚本运行良好,除非我想在不同的位置生成玩家。我根据当前连接的玩家数量确定位置。现在,变量正在服务器上更新,所以很好。但是当客户端加入游戏时,他的玩家数为 0。我想将其设置为从服务器变量连接的当前玩家数。我在搞乱 RPC 函数,但没有运气。
这就是我到目前为止所拥有的。
var playerCount : int = 0;
//player and jump master spawn componets
var playerPrefab : GameObject;
//spawn points
var sp1: Transform;
var sp2: Transform;
var sp3: Transform;
var sp4: Transform;
var sp : Transform;
function OnServerInitialized()
{
Debug.Log("Server initilized");
playerCount++;
spawnPlayer(sp1);
}
function OnPlayerConnected(networkPlayer:NetworkPlayer):void
{
Debug.Log (networkPlayer.guid + " connected");
playerCount++;
//spawnPlayer(sp2);
//spawning player here does not work! Just a black screen no cam perhaps
}
function OnConnectedToServer()
{
//loop though every object in the game
for(var go : GameObject in FindObjectsOfType(GameObject))
{
go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
Debug.Log("Connected to server");
Debug.Log("Player Count: " + playerCount);
if(playerCount ==0)
{
sp = sp1;
}
if(playerCount ==1)
{
sp = sp2;
}
if(playerCount ==2)
{
sp = sp3;
}
if(playerCount ==4)
{
sp = sp4;
}
spawnPlayer(sp);
}
//spawn player prefab
function spawnPlayer(SP : Transform)
{
Network.Instantiate(playerPrefab, SP.position, Quaternion.identity, 0);
}
@RPC
function SendPlayerCount(player : NetworkPlayer )
{
//send playercount to client from server
networkView.RPC("assignvariableRPC",player,playerCount);
}
@RPC
function RecivePlayerCount(count : int)
{
//get playercount from server
//playerCount = playerCount;
playerCount = count;
}