quantum-space-buddies/QSB/Syncs/QSBNetworkTransform.cs

40 lines
1017 B
C#
Raw Normal View History

2022-01-15 04:13:04 +00:00
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;
2022-01-15 04:13:04 +00:00
protected override void UpdatePrevData()
{
_prevPosition = transform.position;
_prevRotation = transform.rotation;
}
protected override bool HasChanged() =>
Vector3.Distance(transform.position, _prevPosition) > PositionChangeThreshold ||
Quaternion.Angle(transform.rotation, _prevRotation) > RotationChangeThreshold;
2022-01-15 04:13:04 +00:00
protected override void Serialize(NetworkWriter writer)
2022-01-15 04:13:04 +00:00
{
writer.Write(transform.position);
writer.Write(transform.rotation);
}
protected override void Deserialize(NetworkReader reader)
2022-01-15 04:13:04 +00:00
{
transform.position = reader.ReadVector3();
transform.rotation = reader.ReadQuaternion();
}
}
}