quantum-space-buddies/QSB/Syncs/QSBNetworkTransform.cs
2022-03-02 19:46:33 -08:00

38 lines
988 B
C#

using Mirror;
using QSB.Utility;
using UnityEngine;
namespace QSB.Syncs;
public class QSBNetworkTransform : QSBNetworkBehaviour
{
protected override float SendInterval => 0.05f;
private const float PositionChangeThreshold = 0.05f;
private const float RotationChangeThreshold = 0.05f;
private Vector3 _prevPosition;
private Quaternion _prevRotation;
protected override bool HasChanged() =>
Vector3.Distance(transform.position, _prevPosition) > PositionChangeThreshold ||
Quaternion.Angle(transform.rotation, _prevRotation) > RotationChangeThreshold;
protected override void Serialize(NetworkWriter writer)
{
writer.Write(transform.position);
writer.Write(transform.rotation);
}
protected override void UpdatePrevData()
{
_prevPosition = transform.position;
_prevRotation = transform.rotation;
}
protected override void Deserialize(NetworkReader reader)
{
transform.position = reader.ReadVector3();
transform.rotation = reader.ReadQuaternion();
}
}