0

我有两个重生点,玩家将在连接时出现。

我需要这样,当其中一个玩家连接到一个点时,任何其他玩家总是会在另一个点产生。

如果有帮助,这里有一些视觉效果:https ://goo.gl/Y0ohZC

在此先感谢,我

4

1 回答 1

1

您可以将这两个可能的生成点添加为空游戏对象。然后我会创建一个布尔数组并将其状态设置为真或假,具体取决于该点是否被占用。这些点不直接存储在这个数组中,所以你应该制作另一个数组。在 C# 中,这大致如下所示:

    public GameObject[] Spots = new GameObject[2]; //Drag the spots in here (In the editor)
bool[] OccupiedSpawnSpots = new bool[2];
int mySpotNumber = -1;

void Start() {
    PhotonNetwork.ConnectUsingSettings ("v1.0.0");
}

void OnGUI() {
    GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
}

void OnJoinedLobby() 
{
    Debug.Log ("Joined the lobby");
    PhotonNetwork.JoinRandomRoom ();
}

void OnPhotonRandomJoinFailed() 
{
    Debug.Log ("No room found. Creating a new one...");
    PhotonNetwork.CreateRoom (null);
}

void OnPhotonPlayerConnected(PhotonPlayer player)
{
    //If we are the MasterClient, send the list to the connected player
    if (PhotonNetwork.isMasterClient)
        GetComponent<PhotonView>().RPC("RPC_SendList", player, OccupiedSpawnSpots);
}

void OnApplicationQuit()
{
    //If I am outside and others want to connect, my spot shouldn't be still set as occupied:
    //I mean if the application quits I'm of course going to be disconnected. 
    //You have to do this in every possibility of getting disconnected or leaving the room
    OccupiedSpawnSpots[mySpotNumber] = false;
    //Send the changed List to the others
    GetComponent<PhotonView>().RPC("RPC_UpdateList", PhotonTargets.All, OccupiedSpawnSpots);
}

[RPC]
void RPC_SendList(bool[] ListOfMasterClient)
{
    OccupiedSpawnSpots = ListOfMasterClient;

    //Get the free one
    if (OccupiedSpawnSpots[0] == false)
    {
        //Spawn your player at 0
        SpawnMyPlayer(0);
        //Set it to occupied
        OccupiedSpawnSpots[0] = true;
    } 
    else //so the other must be free
    {
        //Spawn your player at 1
        SpawnMyPlayer(1);
        //Set it to occupied
        OccupiedSpawnSpots[1] = true;
    }

    //Send the changed List to the others
    GetComponent<PhotonView>().RPC("RPC_UpdateList", PhotonTargets.All, OccupiedSpawnSpots);
}

[RPC]
void RPC_UpdateList(bool[] RecentList)
{
    OccupiedSpawnSpots = RecentList;
}

void SpawnMyPlayer(int SpotNumber) {
    // Check if spawnspots are set OK
    if (Spots == null) {
        Debug.LogError ("SpawnSpots aren't assigned!");
        return;
    }

    mySpotNumber = SpotNumber;

    // The player object for the network
    GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate ("PlayerController", 
                                                                   Spots[SpotNumber].transform.position,
                                                                   Spots[SpotNumber].transform.rotation, 0);

    // Enable a disabled script for *this player only, or all would have the same camera, movement, etc
    //((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;

    // Set a camera just for this player
    //myPlayerGO.transform.FindChild ("Main Camera").gameObject.SetActive (true);

    //standbyCamera.SetActive(false);
}

注意:如果你有两个以上的玩家,它会变得更加困难。这段代码没有优化,所以也许你会找到更好的,但它应该给你正确的想法。

请问您是否有不清楚的地方...

于 2015-05-26T21:52:19.867 回答