我怎么能用光子游戏解决这个问题?

问题描述 投票:-2回答:1

我关注了Expressed Unity的多人FPS系列教程,尤其是这一集"https:/youtu.bej9PC9RhurRI?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)
        {

        }
    }
}

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

c# visual-studio unity3d photon playfab
1个回答
0
投票

解决办法找到了!我只是把sp spawntime改为1秒,而不是0,所以玩家在实例化之前有时间加入房间。

© www.soinside.com 2019 - 2024. All rights reserved.