当一个玩家进入服务器时,以下代码起作用,当第一个玩家进入时一切正常,当第二个玩家进入时,它产生 * 3player(Clone);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
using System.IO;
public class gameManag : MonoBehaviourPunCallbacks
{
// public Text chat;
public GameObject PlayerPf;
public GameObject CameraPf;
public Managmant NAME;
private PhotonView play;
private Vector3[] spawn = new []{new Vector3(0f,19f,65f),new Vector3(80f,19f,65f),new Vector3(80f,-24f,65f),new Vector3(0f,-24f,65f)};
private void Start()
{
PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs","player"),spawn[UnityEngine.Random.Range(0,3)],Quaternion.identity);
// GameObject.Find("player(Clone)").name = PhotonNetwork.NickName;
// PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs","Cam"),new Vector3(GameObject.Find(PhotonNetwork.NickName).transform.position.x,GameObject.Find(PhotonNetwork.NickName).transform.position.y,-30f),Quaternion.identity);
// GameObject.Find("Cam(Clone)").name = PhotonNetwork.NickName+"cam";
}
public override void OnPlayerEnteredRoom(Player newPl)
{
// PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs","player"),spawn[Random.Range(0,3)],Quaternion.identity);
// GameObject.Find("player(Clone)").name = PhotonNetwork.NickName;
// PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs","Cam"),new Vector3(GameObject.Find(PhotonNetwork.NickName).transform.position.x,GameObject.Find(PhotonNetwork.NickName).transform.position.y,-30f),Quaternion.identity);
// GameObject.Find("Cam(Clone)").name = PhotonNetwork.NickName+"cam";
}
}
您的脚本
gameManag
在游戏的所有实例上执行。在第 2 名玩家进入的瞬间,OnPlayerEnteredRoom
由第 1 名玩家执行;Start
和OnPlayerEnteredRoom
第二个玩家。有几种选择可以解决这个问题。OnPlayerEnteredRoom
并编写一个类gameManag
,期望它将为所有玩家执行或者,最好是
gameManag
IsMasterClient
例如,在课堂上进行验证Instantiate
,而不是Pthoton
PS:还请注意,在您的情况下,玩家可以在同一个地方产卵。
PPS:这是一个非常酷的关于 Photon 的俄语教程https://www.youtube.com/watch?v=aqxzrk-pl-8