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);
}
}
}
}