Skip to content

Commit a0231af

Browse files
fix: transform init in OnNetworkSpawn, network transform remove lossy… (#1294)
- Init transform OnNetworkSpawn to handle disabled object spawns - Always sync local scale since "world" (lossy) scale is an approximation
1 parent a968834 commit a0231af

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

com.unity.netcode.gameobjects/Components/NetworkTransform.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
298298
private int m_LastSentTick;
299299
private NetworkTransformState m_LastSentState;
300300

301-
302301
/// <summary>
303302
/// Tries updating the server authoritative transform, only if allowed.
304303
/// If this called server side, this will commit directly.
@@ -396,7 +395,7 @@ internal bool ApplyTransformToNetworkState(ref NetworkTransformState networkStat
396395
{
397396
var position = InLocalSpace ? transformToUse.localPosition : transformToUse.position;
398397
var rotAngles = InLocalSpace ? transformToUse.localEulerAngles : transformToUse.eulerAngles;
399-
var scale = InLocalSpace ? transformToUse.localScale : transformToUse.lossyScale;
398+
var scale = transformToUse.localScale;
400399
return ApplyTransformToNetworkStateWithInfo(ref networkState, dirtyTime, position, rotAngles, scale);
401400
}
402401

@@ -510,7 +509,7 @@ private void ApplyInterpolatedNetworkStateToTransform(NetworkTransformState netw
510509

511510
// todo: we should store network state w/ quats vs. euler angles
512511
var interpolatedRotAngles = InLocalSpace ? transformToUpdate.localEulerAngles : transformToUpdate.eulerAngles;
513-
var interpolatedScale = InLocalSpace ? transformToUpdate.localScale : transformToUpdate.lossyScale;
512+
var interpolatedScale = transformToUpdate.localScale;
514513

515514
// InLocalSpace Read
516515
InLocalSpace = networkState.InLocalSpace;
@@ -599,18 +598,7 @@ private void ApplyInterpolatedNetworkStateToTransform(NetworkTransformState netw
599598
// Scale Apply
600599
if (SyncScaleX || SyncScaleY || SyncScaleZ)
601600
{
602-
if (InLocalSpace)
603-
{
604-
transformToUpdate.localScale = interpolatedScale;
605-
}
606-
else
607-
{
608-
transformToUpdate.localScale = Vector3.one;
609-
var lossyScale = transformToUpdate.lossyScale;
610-
// todo this conversion is messing with interpolation. local scale interpolates fine, lossy scale is jittery. must investigate. MTT-1208
611-
transformToUpdate.localScale = new Vector3(interpolatedScale.x / lossyScale.x, interpolatedScale.y / lossyScale.y, interpolatedScale.z / lossyScale.z);
612-
}
613-
601+
transformToUpdate.localScale = interpolatedScale;
614602
m_PrevNetworkState.Scale = interpolatedScale;
615603
}
616604
}
@@ -679,8 +667,6 @@ private void OnNetworkStateChanged(NetworkTransformState oldState, NetworkTransf
679667

680668
private void Awake()
681669
{
682-
m_Transform = transform;
683-
684670
// we only want to create our interpolators during Awake so that, when pooled, we do not create tons
685671
// of gc thrash each time objects wink out and are re-used
686672
m_PositionXInterpolator = new BufferedLinearInterpolatorFloat();
@@ -704,6 +690,10 @@ private void Awake()
704690

705691
public override void OnNetworkSpawn()
706692
{
693+
// must set up m_Transform in OnNetworkSpawn because it's possible an object spawns but is disabled
694+
// and thus awake won't be called.
695+
// TODO: investigate further on not sending data for something that is not enabled
696+
m_Transform = transform;
707697
m_ReplicatedNetworkState.OnValueChanged += OnNetworkStateChanged;
708698

709699
CanCommitToTransform = IsServer;
@@ -863,6 +853,8 @@ protected virtual void Update()
863853
var oldStateDirtyInfo = ApplyTransformToNetworkStateWithInfo(ref m_PrevNetworkState, 0, m_Transform);
864854

865855
// there are several bugs in this code, as we the message is dumped out under odd circumstances
856+
// For Matt, it would trigger when an object's rotation was perturbed by colliding with another
857+
// object vs. explicitly rotating it
866858
if (oldStateDirtyInfo.isPositionDirty || oldStateDirtyInfo.isScaleDirty || (oldStateDirtyInfo.isRotationDirty && SyncRotAngleX && SyncRotAngleY && SyncRotAngleZ))
867859
{
868860
// ignoring rotation dirty since quaternions will mess with euler angles, making this impossible to determine if the change to a single axis comes

com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ public IEnumerator TestCantChangeTransformFromOtherSideAuthority([Values] bool t
177177

178178
Assert.AreEqual(Vector3.zero, otherSideNetworkTransform.transform.position, "got authority error, but other side still moved!");
179179
#if NGO_TRANSFORM_DEBUG
180+
// We are no longer emitting this warning, and we are banishing tests that rely on console output, so
181+
// needs re-implementation
180182
// TODO: This should be a separate test - verify 1 behavior per test
181183
LogAssert.Expect(LogType.Warning, new Regex(".*without authority detected.*"));
182184
#endif

0 commit comments

Comments
 (0)