Skip to content

Commit 017131d

Browse files
fix: NetworkTransform cannot set non-synchronized axis on non-authority instances (#3471)
Issue verified when investigating [ForumSupport-939437](https://discussions.unity.com/t/client-network-transform-syncing-unchecked-axis/939437/1). This PR resolves the issue in v2.x where you cannot update non-synchronized axis values on non-authority instances without having them be reset by the next frame. [MTTB-1334](https://jira.unity3d.com/browse/MTTB-1334) ## Changelog - Fixed: Issue where non-authority `NetworkTransform` instances would not allow non-synchronized axis values to be updated locally. ## Testing and Documentation - Includes the `NetworkTransformNonAuthorityTests` integration test. - No documentation changes or additions were necessary. <!-- Uncomment and mark items off with a * if this PR deprecates any API: ### Deprecated API - [ ] An `[Obsolete]` attribute was added along with a `(RemovedAfter yyyy-mm-dd)` entry. - [ ] An [api updater] was added. - [ ] Deprecation of the API is explained in the CHANGELOG. - [ ] The users can understand why this API was removed and what they should use instead. --> ## Backport This fix is specific to NGO v2.x.
1 parent 964e0ca commit 017131d

File tree

4 files changed

+648
-4
lines changed

4 files changed

+648
-4
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1414

1515
### Fixed
1616

17+
- Fixed issue where non-authority `NetworkTransform` instances would not allow non-synchronized axis values to be updated locally. (#3471)
1718
- Fixed issue where invoking `NetworkObject.NetworkShow` and `NetworkObject.ChangeOwnership` consecutively within the same call stack location could result in an unnecessary change in ownership error message generated on the target client side. (#3468)
1819
- Fixed issue where `NetworkVariable`s on a `NetworkBehaviour` could fail to synchronize changes if one has `NetworkVariableUpdateTraits` set and is dirty but is not ready to send. (#3466)
1920
- Fixed inconsistencies in the `OnSceneEvent` callback. (#3458)

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

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,10 +2524,25 @@ protected internal void ApplyAuthoritativeState()
25242524
// at the end of this method and assure that when not interpolating the non-authoritative side
25252525
// cannot make adjustments to any portions the transform not being synchronized.
25262526
var adjustedPosition = m_InternalCurrentPosition;
2527-
var adjustedRotation = m_InternalCurrentRotation;
2527+
var currentPosistion = GetSpaceRelativePosition();
2528+
adjustedPosition.x = SyncPositionX ? m_InternalCurrentPosition.x : currentPosistion.x;
2529+
adjustedPosition.y = SyncPositionY ? m_InternalCurrentPosition.y : currentPosistion.y;
2530+
adjustedPosition.z = SyncPositionZ ? m_InternalCurrentPosition.z : currentPosistion.z;
25282531

2532+
var adjustedRotation = m_InternalCurrentRotation;
25292533
var adjustedRotAngles = adjustedRotation.eulerAngles;
2534+
var currentRotation = GetSpaceRelativeRotation().eulerAngles;
2535+
adjustedRotAngles.x = SyncRotAngleX ? adjustedRotAngles.x : currentRotation.x;
2536+
adjustedRotAngles.y = SyncRotAngleY ? adjustedRotAngles.y : currentRotation.y;
2537+
adjustedRotAngles.z = SyncRotAngleZ ? adjustedRotAngles.z : currentRotation.z;
2538+
adjustedRotation.eulerAngles = adjustedRotAngles;
2539+
2540+
25302541
var adjustedScale = m_InternalCurrentScale;
2542+
var currentScale = GetScale();
2543+
adjustedScale.x = SyncScaleX ? adjustedScale.x : currentScale.x;
2544+
adjustedScale.y = SyncScaleY ? adjustedScale.y : currentScale.y;
2545+
adjustedScale.z = SyncScaleZ ? adjustedScale.z : currentScale.z;
25312546

25322547
// Non-Authority Preservers the authority's transform state update modes
25332548
InLocalSpace = networkState.InLocalSpace;
@@ -2650,7 +2665,18 @@ protected internal void ApplyAuthoritativeState()
26502665
// Update our current position if it changed or we are interpolating
26512666
if (networkState.HasPositionChange || Interpolate)
26522667
{
2653-
m_InternalCurrentPosition = adjustedPosition;
2668+
if (SyncPositionX && SyncPositionY && SyncPositionZ)
2669+
{
2670+
m_InternalCurrentPosition = adjustedPosition;
2671+
}
2672+
else
2673+
{
2674+
// Preserve any non-synchronized changes to the local instance's position
2675+
var position = InLocalSpace ? transform.localPosition : transform.position;
2676+
m_InternalCurrentPosition.x = SyncPositionX ? adjustedPosition.x : position.x;
2677+
m_InternalCurrentPosition.y = SyncPositionY ? adjustedPosition.y : position.y;
2678+
m_InternalCurrentPosition.z = SyncPositionZ ? adjustedPosition.z : position.z;
2679+
}
26542680
}
26552681
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
26562682
if (m_UseRigidbodyForMotion)
@@ -2696,7 +2722,21 @@ protected internal void ApplyAuthoritativeState()
26962722
// Update our current rotation if it changed or we are interpolating
26972723
if (networkState.HasRotAngleChange || Interpolate)
26982724
{
2699-
m_InternalCurrentRotation = adjustedRotation;
2725+
if ((SyncRotAngleX && SyncRotAngleY && SyncRotAngleZ) || UseQuaternionSynchronization)
2726+
{
2727+
m_InternalCurrentRotation = adjustedRotation;
2728+
}
2729+
else
2730+
{
2731+
// Preserve any non-synchronized changes to the local instance's rotation
2732+
var rotation = InLocalSpace ? transform.localRotation.eulerAngles : transform.rotation.eulerAngles;
2733+
var currentEuler = m_InternalCurrentRotation.eulerAngles;
2734+
var updatedEuler = adjustedRotation.eulerAngles;
2735+
currentEuler.x = SyncRotAngleX ? updatedEuler.x : rotation.x;
2736+
currentEuler.y = SyncRotAngleY ? updatedEuler.y : rotation.y;
2737+
currentEuler.z = SyncRotAngleZ ? updatedEuler.z : rotation.z;
2738+
m_InternalCurrentRotation.eulerAngles = currentEuler;
2739+
}
27002740
}
27012741

27022742
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
@@ -2737,7 +2777,18 @@ protected internal void ApplyAuthoritativeState()
27372777
// Update our current scale if it changed or we are interpolating
27382778
if (networkState.HasScaleChange || Interpolate)
27392779
{
2740-
m_InternalCurrentScale = adjustedScale;
2780+
if (SyncScaleX && SyncScaleY && SyncScaleZ)
2781+
{
2782+
m_InternalCurrentScale = adjustedScale;
2783+
}
2784+
else
2785+
{
2786+
// Preserve any non-synchronized changes to the local instance's scale
2787+
var scale = transform.localScale;
2788+
m_InternalCurrentScale.x = SyncScaleX ? adjustedScale.x : scale.x;
2789+
m_InternalCurrentScale.y = SyncScaleY ? adjustedScale.y : scale.y;
2790+
m_InternalCurrentScale.z = SyncScaleZ ? adjustedScale.z : scale.z;
2791+
}
27412792
}
27422793
transform.localScale = m_InternalCurrentScale;
27432794
}

0 commit comments

Comments
 (0)