Skip to content

Commit d3347bd

Browse files
fix
NetworkSpawnManager: Relative to spawning only, process deferred messages after post spawn. NetworkTransform: Synchronize the SwitchTransformSpaceWhenParented flag when it changes on the authority side. Perform an early check in CheckForStateChange for changes to SwitchTransformSpaceWhenParented. Apply changes to SwitchTransformSpaceWhenParented when processing a state update on non-authority instance.
1 parent 197f4b0 commit d3347bd

File tree

2 files changed

+54
-28
lines changed

2 files changed

+54
-28
lines changed

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

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public struct NetworkTransformState : INetworkSerializable
5959
private const int k_ReliableSequenced = 0x00080000;
6060
private const int k_UseUnreliableDeltas = 0x00100000;
6161
private const int k_UnreliableFrameSync = 0x00200000;
62+
private const int k_SwitchTransformSpaceWhenParented = 0x0400000;
6263
// (Internal Debugging) When set each state update will contain a state identifier
6364
private const int k_TrackStateId = 0x10000000;
6465

@@ -123,6 +124,19 @@ internal uint BitSet
123124
private FastBufferReader m_Reader;
124125
private FastBufferWriter m_Writer;
125126

127+
/// <summary>
128+
/// When set, non-authority instances will smoothly transition between
129+
/// world and local space.
130+
/// </summary>
131+
public bool SwitchTransformSpaceWhenParented
132+
{
133+
get => GetFlag(k_SwitchTransformSpaceWhenParented);
134+
internal set
135+
{
136+
SetFlag(value, k_SwitchTransformSpaceWhenParented);
137+
}
138+
}
139+
126140
/// <summary>
127141
/// When set, the <see cref="NetworkTransform"/> is operates in local space
128142
/// </summary>
@@ -518,7 +532,7 @@ private void SetFlag(bool set, int flag)
518532
internal void ClearBitSetForNextTick()
519533
{
520534
// Clear everything but flags that should persist between state updates until changed by authority
521-
m_Bitset &= k_InLocalSpaceBit | k_Interpolate | k_UseHalfFloats | k_QuaternionSync | k_QuaternionCompress | k_PositionSlerp | k_UseUnreliableDeltas;
535+
m_Bitset &= k_InLocalSpaceBit | k_Interpolate | k_UseHalfFloats | k_QuaternionSync | k_QuaternionCompress | k_PositionSlerp | k_UseUnreliableDeltas | k_SwitchTransformSpaceWhenParented;
522536
IsDirty = false;
523537
}
524538

@@ -2059,30 +2073,15 @@ private bool CheckForStateChange(ref NetworkTransformState networkState, ref Tra
20592073
var isPositionDirty = isTeleportingAndNotSynchronizing ? networkState.HasPositionChange : false;
20602074
var isRotationDirty = isTeleportingAndNotSynchronizing ? networkState.HasRotAngleChange : false;
20612075
var isScaleDirty = isTeleportingAndNotSynchronizing ? networkState.HasScaleChange : false;
2076+
if (SwitchTransformSpaceWhenParented != networkState.SwitchTransformSpaceWhenParented)
2077+
{
2078+
//isDirty = true;
2079+
//forceState = true;
2080+
//networkState.IsTeleportingNextFrame = SwitchTransformSpaceWhenParented;
20622081

2063-
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
2064-
var position = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetPosition() : InLocalSpace ? transformToUse.localPosition : transformToUse.position;
2065-
var rotation = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetRotation() : InLocalSpace ? transformToUse.localRotation : transformToUse.rotation;
2066-
2067-
var positionThreshold = Vector3.one * PositionThreshold;
2068-
var rotationThreshold = Vector3.one * RotAngleThreshold;
2069-
2070-
// NSS: Disabling this for the time being
2071-
// TODO: Determine if we actually need this and if not remove this from NetworkRigidBodyBase
2072-
//if (m_UseRigidbodyForMotion)
2073-
//{
2074-
// positionThreshold = m_NetworkRigidbodyInternal.GetAdjustedPositionThreshold();
2075-
// rotationThreshold = m_NetworkRigidbodyInternal.GetAdjustedRotationThreshold();
2076-
//}
2077-
#else
2078-
var position = InLocalSpace ? transformToUse.localPosition : transformToUse.position;
2079-
var rotation = InLocalSpace ? transformToUse.localRotation : transformToUse.rotation;
2080-
var positionThreshold = Vector3.one * PositionThreshold;
2081-
var rotationThreshold = Vector3.one * RotAngleThreshold;
2082-
#endif
2083-
var rotAngles = rotation.eulerAngles;
2084-
var scale = transformToUse.localScale;
2085-
networkState.IsSynchronizing = isSynchronization;
2082+
networkState.InLocalSpace = SwitchTransformSpaceWhenParented ? transform.parent != null : InLocalSpace;
2083+
}
2084+
networkState.SwitchTransformSpaceWhenParented = SwitchTransformSpaceWhenParented;
20862085

20872086
// All of the checks below, up to the delta position checking portion, are to determine if the
20882087
// authority changed a property during runtime that requires a full synchronizing.
@@ -2108,6 +2107,31 @@ private bool CheckForStateChange(ref NetworkTransformState networkState, ref Tra
21082107
forceState = SwitchTransformSpaceWhenParented;
21092108
}
21102109

2110+
2111+
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
2112+
var position = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetPosition() : InLocalSpace ? transformToUse.localPosition : transformToUse.position;
2113+
var rotation = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetRotation() : InLocalSpace ? transformToUse.localRotation : transformToUse.rotation;
2114+
2115+
var positionThreshold = Vector3.one * PositionThreshold;
2116+
var rotationThreshold = Vector3.one * RotAngleThreshold;
2117+
2118+
// NSS: Disabling this for the time being
2119+
// TODO: Determine if we actually need this and if not remove this from NetworkRigidBodyBase
2120+
//if (m_UseRigidbodyForMotion)
2121+
//{
2122+
// positionThreshold = m_NetworkRigidbodyInternal.GetAdjustedPositionThreshold();
2123+
// rotationThreshold = m_NetworkRigidbodyInternal.GetAdjustedRotationThreshold();
2124+
//}
2125+
#else
2126+
var position = InLocalSpace ? transformToUse.localPosition : transformToUse.position;
2127+
var rotation = InLocalSpace ? transformToUse.localRotation : transformToUse.rotation;
2128+
var positionThreshold = Vector3.one * PositionThreshold;
2129+
var rotationThreshold = Vector3.one * RotAngleThreshold;
2130+
#endif
2131+
var rotAngles = rotation.eulerAngles;
2132+
var scale = transformToUse.localScale;
2133+
networkState.IsSynchronizing = isSynchronization;
2134+
21112135
// Check for parenting when synchronizing and/or teleporting
21122136
if (isSynchronization || networkState.IsTeleportingNextFrame)
21132137
{
@@ -3038,6 +3062,8 @@ internal void ApplyUpdatedState(NetworkTransformState newState)
30383062
UseHalfFloatPrecision = newState.UseHalfFloatPrecision;
30393063
UseUnreliableDeltas = newState.UseUnreliableDeltas;
30403064

3065+
SwitchTransformSpaceWhenParented = newState.SwitchTransformSpaceWhenParented;
3066+
30413067
if (SlerpPosition != newState.UsePositionSlerp)
30423068
{
30433069
SlerpPosition = newState.UsePositionSlerp;
@@ -3828,7 +3854,6 @@ public override void OnNetworkObjectParentChanged(NetworkObject parentNetworkObj
38283854
base.OnNetworkObjectParentChanged(parentNetworkObject);
38293855
}
38303856

3831-
38323857
internal override void InternalOnNetworkObjectParentChanged(NetworkObject parentNetworkObject)
38333858
{
38343859
// The root NetworkTransform handles tracking any NetworkObject parenting since nested NetworkTransforms (of the same NetworkObject)
@@ -4781,6 +4806,6 @@ private static void DeregisterForTickUpdate(NetworkTransform networkTransform)
47814806

47824807
internal interface INetworkTransformLogStateEntry
47834808
{
4784-
void AddLogEntry(NetworkTransform.NetworkTransformState networkTransformState, ulong targetClient, bool preUpdate = false);
4809+
public void AddLogEntry(NetworkTransform.NetworkTransformState networkTransformState, ulong targetClient, bool preUpdate = false);
47854810
}
47864811
}

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,9 @@ internal void SpawnNetworkObjectLocally(NetworkObject networkObject, in NetworkO
11481148

11491149
// It is ok to invoke NetworkBehaviour.OnPostSpawn methods
11501150
networkObject.InvokeBehaviourNetworkPostSpawn();
1151+
1152+
// Process any deferred messages once the object is 100% finished spawning,
1153+
NetworkManager.DeferredMessageManager.ProcessTriggers(IDeferredNetworkMessageManager.TriggerType.OnSpawn, networkObject.NetworkObjectId);
11511154
}
11521155

11531156
private void SpawnNetworkObjectLocallyCommon(NetworkObject networkObject, ulong networkId, bool sceneObject, bool playerObject, ulong ownerClientId, bool destroyWithScene)
@@ -1224,8 +1227,6 @@ private void SpawnNetworkObjectLocallyCommon(NetworkObject networkObject, ulong
12241227

12251228
networkObject.InvokeBehaviourNetworkSpawn();
12261229

1227-
NetworkManager.DeferredMessageManager.ProcessTriggers(IDeferredNetworkMessageManager.TriggerType.OnSpawn, networkId);
1228-
12291230
// propagate the IsSceneObject setting to child NetworkObjects
12301231
var children = networkObject.GetComponentsInChildren<NetworkObject>();
12311232
foreach (var childObject in children)

0 commit comments

Comments
 (0)