【问题标题】:How could i solve this problem with photon game我怎么能用光子游戏解决这个问题
【发布时间】:2020-09-29 14:00:23
【问题描述】:

我已关注 Expressed Unity 的多人 fps 系列教程,尤其是这一集“https://youtu.be/j9PC9RhurRI?list=PLD4OdGjxbaByCEOH3fOJ4MgOdROHHBKUo”,我需要一些帮助。

我一直关注视频直到 23:30,然后所有的东西都坏了。我收到错误消息“在客户端加入/创建房间之前无法实例化。状态:加入。”我不知道我该怎么办。

我已经检查了所有代码和所有内容,但一无所获。你有解决办法吗?我不知道哪个代码有问题,所以我复制了我在这个视频之后编辑的所有三个代码。

MpManager 脚本:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.SceneManagement;

public class MPManager : MonoBehaviourPunCallbacks
{

    public GameObject[] EnableObjectsOnConnect;
    public GameObject[] DisableObjectsOnConnect;

    // Start is called before the first frame update
    void Start()
    {
        PhotonNetwork.ConnectUsingSettings();
        //PhotonNetwork.ConnectToRegion("eu");
    }


    public override void OnConnectedToMaster()
    {
        foreach(GameObject obj in EnableObjectsOnConnect)
        {
            obj.SetActive(true);
        }
        foreach(GameObject obj in DisableObjectsOnConnect)
        {
            obj.SetActive(false);
        }
        Debug.Log("Connected to photon");
    }

    public void JoinFFA()
    {
        PhotonNetwork.AutomaticallySyncScene = true;
        PhotonNetwork.JoinRandomRoom();
    }

    public override void OnJoinRandomFailed(short returnCode, string message)
    {
        CreateFFA();
    }

    public void CreateFFA()
    {
        PhotonNetwork.AutomaticallySyncScene = true;

        RoomOptions ro = new RoomOptions { MaxPlayers = 10, IsOpen = true, IsVisible = true };
        PhotonNetwork.CreateRoom("defaultFFA", ro, TypedLobby.Default);

        SceneManager.LoadScene("FFA");
    }
}

运动脚本:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon;
using Photon.Pun;

public class Movement : MonoBehaviourPun
{
    public KeyCode Left;
    public KeyCode Right;
    public KeyCode Forward;
    public KeyCode Backward;

    [SerializeField]
    private float MoveSpeed = 50;

    private Rigidbody body;
    private GameObject cam;

    // Start is called before the first frame update
    void Start()
    {
        body = GetComponent<Rigidbody>();
        cam = gameObject.transform.GetChild(0).gameObject;
        if (photonView.IsMine)
        {
            cam.SetActive(true);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (photonView.IsMine)
        {
            float x = Input.GetAxis("Mouse X");
            float y = Input.GetAxis("Mouse Y");

            if (Input.GetKey(Left))
            {
                body.AddRelativeForce(Vector3.left * MoveSpeed, ForceMode.Impulse);
            }

            if (Input.GetKey(Right))
            {
                body.AddRelativeForce(Vector3.left * -MoveSpeed, ForceMode.Impulse);
            }

            if (Input.GetKey(Forward))
            {
                body.AddRelativeForce(Vector3.forward * MoveSpeed, ForceMode.Impulse);
            }

            if (Input.GetKey(Backward))
            {
                body.AddRelativeForce(Vector3.forward * -MoveSpeed, ForceMode.Impulse);
            }
            gameObject.transform.Rotate(new Vector3(0, x, 0));
            cam.transform.Rotate(new Vector3(-y, 0, 0));
        }
    }
}

FFa 脚本:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon;
using Photon.Pun;

public class FFA : MonoBehaviourPun, IPunObservable
{

    public float SpawnTime;
    float timer;
    bool HasPlayerSpawned = false;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        timer += Time.deltaTime;
        if(timer >= SpawnTime)
        {
            if (!HasPlayerSpawned)
            {
                PhotonNetwork.Instantiate("Player", Vector3.zero, Quaternion.identity, 0);
                HasPlayerSpawned = true;
            }

            timer = 0;
        }
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if(stream.IsWriting)
        {

        }else if (stream.IsReading)
        {

        }
    }
}

对不起,如果我有错别字,我的英语不好。

【问题讨论】:

  • 问题中不得使用不必要的外部链接,请仅发布引发错误的代码而不是所有脚本
  • 请提供您的问题的minimal reproducible example
  • 您的具体问题是什么?你试图解决什么?究竟是什么没有按预期“工作”?请不要链接到帖子,而是在此处复制完整的问题......如果由于某种原因外部链接破坏了您的帖子变得无用
  • 帖子编辑完毕!

标签: c# visual-studio unity3d photon playfab


【解决方案1】:

找到解决方案!我只是将生成时间设置为 1 秒而不是 0,这样玩家就有时间在实例化之前加入房间。

【讨论】:

    猜你喜欢
    • 2011-04-29
    • 2013-10-24
    • 1970-01-01
    • 2015-04-07
    • 2019-04-10
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2019-02-20
    相关资源
    最近更新 更多