c# - 使用 Unity Photon 将玩家位置发送给另一个玩家以在多人游戏中移动他

标签 c# unity3d photon

我是按照PUN基础教程的步骤来的。 现在我达到了我想要的一部分将我当前的位置发送给房间里我的另一个玩家以移动他。我可以在每次更新时打印我的位置,我需要的是知道如何将位置发送给另一个玩家以移动他。 假设我有一个桌面播放器,当我移动他时,这个翻译移动了移动播放器。 以及我如何停止在移动设备上实例化对象,我只想处理桌面上的实例化对象。 我正在使用 unity 和 Photon Network SDK。

这是我使用的代码

using UnityEngine;
using System.Collections;

public class NetworkCharacter : Photon.PunBehaviour {

    private Vector3 correctPlayerPos;

    void Update()
    {
        if (!photonView.isMine)
            transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);

        photonView.RPC ("PrintPosition", PhotonTargets.All, transform.position);
    }

    void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.isWriting)
        {
            // We own this player: send the others our data
            stream.SendNext(transform.position);
        }
        else
        {
            // Network player, receive data
            this.correctPlayerPos = (Vector3)stream.ReceiveNext();
        }
    }

    [PunRPC]
    void PrintPosition(Vector3 pos)
    {
        Debug.Log (pos);
        //I need to send position coordinates to the other device
    }
}

创建多人环境的另一类:

using UnityEngine;
using System.Collections;

public class NetworkManager : Photon.PunBehaviour {

    // Use this for initialization
    void Start () {
        PhotonNetwork.ConnectUsingSettings ("0.1");
        //PhotonNetwork.logLevel = PhotonLogLevel.Full;
    }

    void Update () {

    }

    void OnGUI()
    {
        GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
    }

    public override void OnJoinedLobby ()
    {
        Debug.Log("join lobby!");
        PhotonNetwork.JoinRandomRoom ();
    }

    void OnPhotonRandomJoinFailed()
    {
        Debug.Log("Can't join random room!");
        PhotonNetwork.CreateRoom(null);
    }

    void OnJoinedRoom()
    {
        Debug.Log("join random room!");
        GameObject monster = PhotonNetwork.Instantiate("monsterprefab", Vector3.zero, Quaternion.identity, 0);
    }
}

最佳答案

PUN 自更新以来有一些新组件。 我建议您使用这些组件,因为它对新用户非常友好。

您可以使用的一些组件:

  • 光子变换 View
  • 光子刚体 View
  • PhotonAnimatorView

这三个组件将帮助您在它附加到的游戏对象上同步您的位置、物理和动画。

如果您不想使用这些组件,我建议您搜索:Unity PUN 的插值和外推。

一些不错的入门教程:

  • SkyArena PUN Tutorial这是关于 PUN 的非常好的视频教程[部分],因此您一定要检查一下。
  • Exit Games Marco Polo本教程由来自 PUN 的人创建,即使您不需要它也非常适合阅读。

希望对您有所帮助。

-门诺

关于c# - 使用 Unity Photon 将玩家位置发送给另一个玩家以在多人游戏中移动他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40330856/

相关文章:

c# - 如果状态码不等于 200 OK,则使用 Polly 重试 api 请求

javascript - 具有统一面的程序圆形网格

c# - 操作系统设计问题 : File types associated with their appropriate programs

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

unity-game-engine - 如何使用 Photon Engine 阻止特定用户进行随机匹配?

c# - 到 "broadcast"到 TCP 客户端列表的最快方法

c# - 有没有更有效的方法在 C# 中创建我的 DataTable?

c# - 如何通过 photon unity network - unity 3d 实例化预制件来生成对象

c# - 获取 EventHub PartitionId 列表时遇到错误。微软AzureAmqp

c# - 统一 3D : Footstep sounds looping the first few milliseconds instead of playing the full sound then looping