c# - 如何使用光子统一在固定位置生成多人游戏用户?

标签 c# unity3d photon

我一直在为多人游戏使用 Photon 统一网络插件。在下面的角色实例化代码中,我想生成加入的玩家,然后在固定点随机生成。我是新手,我试图通过在按钮单击事件中提供固定的 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);
        }
    }
  }
}

最佳答案

如果你想制作不同的生成点,你应该将你的脚本更改为:

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 提供值。

关于c# - 如何使用光子统一在固定位置生成多人游戏用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43694455/

相关文章:

c# - Findcontrol 属性在 createUserWizard 中不起作用

c# - 如何检查通用类型 T 是否为 IEnumerable<T'>,其中 T' 未知

c# - 如何在 Unity Editor 脚本中将 Persistent Listener 添加到 Button.onClick 事件

Android 构建失败 - FormatException

c# - 光子统一 foreach 循环

c# - 如何在 ios unity 后台运行代码

c# - "nameof "运算符 ,"expression cannot be used in an argument to nameof"

c# - Unity HTC VIVE 远距传送

unity3d - PlayFab vs Photon vs 其他解决方案

c# - EventToCommand 和 CommandManagerBinding 不工作