2022-01-15 09:54:02 +00:00
|
|
|
|
using Mirror;
|
|
|
|
|
using QSB.Utility;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2022-02-25 06:04:54 +00:00
|
|
|
|
namespace QSB.Syncs;
|
|
|
|
|
|
|
|
|
|
public class QSBNetworkTransformChild : QSBNetworkBehaviour
|
2022-01-15 09:54:02 +00:00
|
|
|
|
{
|
2022-02-25 06:04:54 +00:00
|
|
|
|
public Transform Target;
|
2022-01-15 09:54:02 +00:00
|
|
|
|
|
2022-02-25 06:04:54 +00:00
|
|
|
|
protected override float SendInterval => 0.05f;
|
2022-01-15 09:54:02 +00:00
|
|
|
|
|
2022-02-25 06:04:54 +00:00
|
|
|
|
private const float PositionChangeThreshold = 0.05f;
|
|
|
|
|
private const float RotationChangeThreshold = 0.05f;
|
2022-02-17 04:11:05 +00:00
|
|
|
|
|
2022-02-25 06:04:54 +00:00
|
|
|
|
private Vector3 _prevPosition;
|
|
|
|
|
private Quaternion _prevRotation;
|
2022-01-15 09:54:02 +00:00
|
|
|
|
|
2022-02-25 06:04:54 +00:00
|
|
|
|
protected override void UpdatePrevData()
|
|
|
|
|
{
|
|
|
|
|
_prevPosition = Target.localPosition;
|
|
|
|
|
_prevRotation = Target.localRotation;
|
|
|
|
|
}
|
2022-01-15 09:54:02 +00:00
|
|
|
|
|
2022-02-25 06:04:54 +00:00
|
|
|
|
protected override bool HasChanged() =>
|
|
|
|
|
Vector3.Distance(Target.localPosition, _prevPosition) > PositionChangeThreshold ||
|
|
|
|
|
Quaternion.Angle(Target.localRotation, _prevRotation) > RotationChangeThreshold;
|
2022-01-15 09:54:02 +00:00
|
|
|
|
|
2022-02-25 06:04:54 +00:00
|
|
|
|
protected override void Serialize(NetworkWriter writer)
|
|
|
|
|
{
|
|
|
|
|
writer.Write(Target.localPosition);
|
|
|
|
|
writer.Write(Target.localRotation);
|
|
|
|
|
}
|
2022-01-15 09:54:02 +00:00
|
|
|
|
|
2022-02-25 06:04:54 +00:00
|
|
|
|
protected override void Deserialize(NetworkReader reader)
|
|
|
|
|
{
|
|
|
|
|
Target.localPosition = reader.ReadVector3();
|
|
|
|
|
Target.localRotation = reader.ReadQuaternion();
|
2022-01-15 09:54:02 +00:00
|
|
|
|
}
|
2022-02-25 06:04:54 +00:00
|
|
|
|
}
|