【发布时间】: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