warp when packet arrives (in OnDeserialize())

This commit is contained in:
in0finite 2022-05-01 03:22:10 +02:00
parent 961a7e601f
commit a99c737f22
2 changed files with 21 additions and 20 deletions

View file

@ -130,19 +130,11 @@ namespace SanAndreasUnity.Net
var rb = this.GetComponentInChildren<Rigidbody>();
// Because first sync will send position/rotation for this transform, we need to assign it's
// position/rotation to the child's transform, which is the transform that will be synced from
// now on. We do this to maintain same behaviour as on server.
// we need to apply initial sync data to child transform that will be synced, because it was not
// applied before (synced trasform was not assigned)
if (rb != null)
{
rb.transform.localPosition = this.transform.localPosition;
rb.transform.localRotation = this.transform.localRotation;
}
this.transform.localPosition = Vector3.zero;
this.transform.localRotation = Quaternion.identity;
this.NetworkTransform.TransformSyncer.GetLatestSyncData().Apply(rb.transform);
this.NetworkTransform.ChangeSyncedTransform(rb != null ? rb.transform : null);
if (rb != null)

View file

@ -59,6 +59,12 @@ namespace SanAndreasUnity.Net
// these are the velocities used to move the object, calculated when new server data arrives
public float CalculatedVelocityMagnitude;
public float CalculatedAngularVelocityMagnitude;
public void Apply(Transform tr)
{
tr.localPosition = this.Position;
tr.localRotation = this.Rotation;
}
}
private readonly bool m_hasTransform = false;
@ -85,7 +91,7 @@ namespace SanAndreasUnity.Net
// apply initial sync data
// first sync should've been done before calling this function, so the data is available
this.WarpToLatestSyncData();
}
public bool OnSerialize(NetworkWriter writer, bool initialState)
@ -114,6 +120,8 @@ namespace SanAndreasUnity.Net
syncData.CalculatedAngularVelocityMagnitude = float.PositiveInfinity;
m_currentSyncData = syncData;
this.WarpToLatestSyncData();
}
else
{
@ -222,22 +230,23 @@ namespace SanAndreasUnity.Net
public void WarpToLatestSyncData()
{
var syncData = m_nextSyncData ?? m_currentSyncData;
var syncData = this.GetLatestSyncData();
this.SetPosition(syncData.Position);
this.SetRotation(syncData.Rotation);
// also assign position/rotation directly to transform, because rigid body may not warp
// assign position/rotation directly to transform, because rigid body may not warp ?
if (m_hasTransform)
{
m_transform.localPosition = syncData.Position;
m_transform.localRotation = syncData.Rotation;
syncData.Apply(m_transform);
}
m_currentSyncData = syncData;
m_nextSyncData = null;
}
public SyncData GetLatestSyncData()
{
return m_nextSyncData ?? m_currentSyncData;
}
private void SetPosition()
{
this.SetPosition(m_currentSyncData.Position);