Replace NetworkTransfrom

本文介绍了一个基于Unity的网络同步机制实现方案,通过自定义基类`BaseSync`及派生类`SyncPosition`和`SyncRotation`来实现游戏对象位置和旋转的平滑同步。该机制利用了`NetworkBehaviour`,并支持客户端和服务器之间的状态同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.AI;

namespace SwordmenWorld
{
    public abstract class BaseSync : NetworkBehaviour
    {
        public float speed = 4f;
        public float threshold = 0.1f;

        [Range(1, 10)]
        public float sendRate = 9;
        float interval;
        float lastTime;
        NavMeshAgent agent;
        public void Start()
        {

            interval = 1.0f / sendRate;

            if (!isLocalPlayer && !hasAuthority)
            {
                agent = GetComponent<NavMeshAgent>();
                if (agent != null)
                {
                    StartCoroutine(WaitForSetAgent());
                }
            }
        }


        IEnumerator WaitForSetAgent()
        {
            while (agent == null)
            {
                yield return 0;
            }
            agent.enabled = false;
        }


        public void FixedUpdate()
        {
            if (Time.time - lastTime > interval)
            {
                Send();
                lastTime = Time.time;
            }
        }

        public void Update()
        {
            Lerp();
        }


        [ClientCallback]
        public abstract void Send();
        public abstract void Lerp();
    }

}


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


namespace SwordmenWorld
{
    public class SyncPosition : BaseSync
    {
        [SyncVar] private Vector3 syncPos;
        //Queue<Vector3> waypoints = new Queue<Vector3>();
        Vector3 lastPos;
        public override void Lerp()
        {
            if (!isLocalPlayer && !hasAuthority)
            {
                //transform.position = Vector3.Lerp(transform.position,syncPos,lerpSpeed);
                transform.position = Vector3.MoveTowards(transform.position, syncPos, speed * Time.deltaTime);
            }
        }


        [Command]
        public void CmdSendToServer(Vector3 pos)
        {
            syncPos = pos;

        }

        [Client]
        public override void Send()
        {
            if (isLocalPlayer || hasAuthority)
            {
                if (Vector3.Distance(transform.position, lastPos) > threshold)
                {
                    CmdSendToServer(transform.position);
                    lastPos = transform.position;
                }
            }
        }
    }
}

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


namespace SwordmenWorld
{
    public class SyncRotation : BaseSync
    {

        public float rotSpeed = 180;

        [SyncVar] Vector3 syncEuler;

        Quaternion lastRotation;


        [Command]
        void CmdSendToServer(Vector3 eular)
        {
            syncEuler = eular;
        }
        public override void Send()
        {
            if (isLocalPlayer || hasAuthority)
            {
                if (Quaternion.Angle(transform.rotation, lastRotation) > threshold)
                {
                    CmdSendToServer(transform.eulerAngles);
                    lastRotation = transform.rotation;
                }
            }
        }


        public override void Lerp()
        {
            if (!isLocalPlayer && !hasAuthority)
            {
                transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(syncEuler), speed);
                //transform.eulerAngles = Vector3.RotateTowards(transform.eulerAngles, syncEuler, Mathf.Deg2Rad * rotSpeed * Time.deltaTime, 0);
            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值