【问题标题】:Stucked In Unity 5 UNET Multiplayer Code卡在 Unity 5 UNET 多人游戏代码中
【发布时间】:2016-08-28 10:36:45
【问题描述】:

我在使用 unity 5 UNET 编写一些多人游戏时卡住了。 目前正在使用 unity 5.1,并希望通过网络同步一些游戏对象的子变换。

问题是所有客户端都在发送请求以将其子转换同步到服务器。但服务器没有将其子转换与其他客户端一起下沉。

我听说该工作有一个组件是 unity 5.2,但正如我告诉你的那样,我正在使用 unity 5.1,所以我想要 5.1 的解决方案。

这是我的一段代码。

期待回复

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class Sync_Position : NetworkBehaviour 
{
    [SyncVar]
    Vector3 []sync_pos;

    [SerializeField] Transform []obj;
    [SerializeField] int lerp_rate;

    Vector3 []last_pos;
    float threshold=5;

    void Start()
    {
        sync_pos= new Vector3[obj.Length];
        last_pos= new Vector3[obj.Length];
    }

    // Update is called once per frame
    void FixedUpdate () 
    {
        transform_pos();
        lerp_pos();
    }

    void lerp_pos()
    {
        if(!isLocalPlayer)
        {
            for(int i=0;i<obj.Length;i++)
            {
                if(Vector3.Distance(obj[i].localPosition,sync_pos[i]) > 1)
                {
                    obj[i].localPosition=sync_pos[i];
                }
            }
        }
    }

    [Command]
    void Cmd_for_pos(Vector3 p, int index)
    {
        sync_pos[index]=p;
    }

    [ClientCallback]
    void transform_pos()
    {
        if(isLocalPlayer)
        {
            for(int i=0;i<obj.Length;i++)
            {
                if(Vector3.Distance(obj[i].localPosition,last_pos[i]) > threshold)
                {
                    Cmd_for_pos(obj[i].localPosition,i);
                    last_pos[i]=obj[i].localPosition;
                }
            }
        }
    }
}

【问题讨论】:

    标签: c# networking unity3d multiplayer


    【解决方案1】:

    您可以在服务器上为每个连接的客户端/播放器创建“影子对象”,然后移动这些对象来代表 antoehr 远程对象的位置。

    我只能推荐查看本教程,因为这是我自己进入 UNET 的。

    https://www.youtube.com/watch?v=NLnzlwCRjgc
    

    【讨论】:

      猜你喜欢
      • 2016-10-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2015-10-20
      • 2018-02-05
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多