using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Move : NetworkBehaviour
{
//[SyncVar(hook = "SetPos")]
[SyncVar]
private Vector3 v3_PlayerPos;
[SyncVar]
private Quaternion qua_PlayerRotate;
private float my_Speed = 10f;
private float my_RotateSpeed = 60f;
void Update()
{
if (!isLocalPlayer)
{
return;
}
float ver = Input.GetAxis("Vertical");
float Hor = Input.GetAxis("Horizontal");
transform.Translate(Hor * Time.deltaTime * my_Speed, 0, ver * my_Speed * Time.deltaTime);
transform.Rotate(new Vector3(0, my_RotateSpeed * Hor * Time.deltaTime, 0));
}
void FixedUpdate()
{
//如果是本地创建 则把数据更新至服务器 通过SyncVar 发送给所有的客户端
if (isLocalPlayer)
{
CmdSendServerPos(transform.position,transform.rotation);
}
else
{
//如果不是本地创建 则 插值移动(所谓的镜像移动)
LerpPosition();
//Debug.Log("2222222");
}
}
//插值移动
void LerpPosition()
{
transform.position = Vector3.Lerp(transform.position, v3_PlayerPos, 5 * Time.fixedDeltaTime);
transform.rotation = Quaternion.Lerp(transform.rotation,qua_PlayerRotate,5 * Time.fixedDeltaTime);
}
hook 调用
//public void SetPos(Vector3 pos)
//{
// v3_PlayerPos = pos;
//}
[Command]
public void CmdSendServerPos(Vector3 pos,Quaternion rotate)
{
v3_PlayerPos = pos;
qua_PlayerRotate = rotate;
}
}
【Unet】不使用NetWorkTransform组件 进行同步位移和旋转
最新推荐文章于 2024-08-07 10:32:47 发布