如何使用光子在固定的loaction中产生多人游戏?

时间:2017-04-29 10:21:34

标签: c# unity3d unity5 photon

我一直在为多人游戏使用Photon unity网络插件。在下面的字符实例化代码中,我想生成加入的玩家,然后在固定点产生然后随机。我是新手,我试图通过在按钮点击事件中给出固定的gameObject位置来编辑它但是无法这样做。这是代码 -

using UnityEngine;
public class CharacterInstantiation : OnJoinedInstantiate {
    public delegate void OnCharacterInstantiated(GameObject character);
    public static event OnCharacterInstantiated CharacterInstantiated;
    public new void OnJoinedRoom() {
        if (this.PrefabsToInstantiate != null) {
            GameObject o = PrefabsToInstantiate[(PhotonNetwork.player.ID - 1) % 4];
            //Debug.Log("Instantiating: " + o.name);
            Vector3 spawnPos = Vector3.zero;
            if (this.SpawnPosition != null) {
                spawnPos = this.SpawnPosition.position;
            }
            Vector3 random = Random.insideUnitSphere;
            random = this.PositionOffset * random.normalized;
            spawnPos += random;
            spawnPos.y = 0;
            Camera.main.transform.position += spawnPos;

            o = PhotonNetwork.Instantiate(o.name, spawnPos, Quaternion.identity, 0);
            if (CharacterInstantiated != null) {
                CharacterInstantiated(o);
            }
        }
    }
}

此代码在插件的测试场景中。只想产生加入的玩家是固定点,如spawnpoint [0],spawnpoint [1]等。在此先感谢您的帮助。

以下是插件中预制实例化的代码 -

 public class OnJoinedInstantiate : MonoBehaviour
{
   public Transform SpawnPosition;
   public float PositionOffset = 2.0f;
   public GameObject[] PrefabsToInstantiate;  
public void OnJoinedRoom()
{
    if (this.PrefabsToInstantiate != null)
    {
        foreach (GameObject o in this.PrefabsToInstantiate)
        {
            Debug.Log("Instantiating: " + o.name);

            Vector3 spawnPos = Vector3.up;
            if (this.SpawnPosition != null)
            {
                spawnPos = this.SpawnPosition.position;
            }

            Vector3 random = Random.insideUnitSphere;
            random.y = 0;
            random = random.normalized;
            Vector3 itempos = spawnPos + this.PositionOffset * random;

            PhotonNetwork.Instantiate(o.name, itempos, Quaternion.identity, 0);
        }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

如果要制作不同的生成点,则应将脚本更改为:

using UnityEngine;

public class CharacterInstantiation : OnJoinedInstantiate {

    public delegate void OnCharacterInstantiated(GameObject character);

    public static event OnCharacterInstantiated CharacterInstantiated;
    public int counter = 0;
    public Vector3[] spawnPositions;
    public new void OnJoinedRoom() {
        if (this.PrefabsToInstantiate != null) {
            GameObject o = PrefabsToInstantiate[(PhotonNetwork.player.ID - 1) % 4];
            //Debug.Log("Instantiating: " + o.name);
            Vector3 spawnPos = Vector3.zero;
            if (this.SpawnPosition != null) {
                spawnPos = spawnPositions[counter];
            }
            Vector3 random = Random.insideUnitSphere;
            random = this.PositionOffset * random.normalized;
            spawnPos += random;
            spawnPos.y = 0;
            Camera.main.transform.position += spawnPos;

            o = PhotonNetwork.Instantiate(o.name, spawnPos, Quaternion.identity, 0);
            if (CharacterInstantiated != null) {
                CharacterInstantiated(o);
                counter++;
            }
        }
    }
}

您只需要为spawnPositions提供值。