Skip to content

fix: NetworkAnimator not synchronizing changes to layer weights #2399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
- Network prefabs are now stored in a ScriptableObject that can be shared between NetworkManagers, and have been exposed for public access. By default, a Default Prefabs List is created that contains all NetworkObject prefabs in the project, and new NetworkManagers will default to using that unless that option is turned off in the Netcode for GameObjects settings. Existing NetworkManagers will maintain their existing lists, which can be migrated to the new format via a button in their inspector. (#2322)

### Fixed
- Fixed issue where changes to a layer's weight would not synchronize unless a state transition was occurring.(#2399)
- Fixed issue where `NetworkManager.LocalClientId` was returning the `NetworkTransport.ServerClientId` as opposed to the `NetworkManager.m_LocalClientId`. (#2398)
- Fixed a UTP test that was failing when you install Unity Transport package 2.0.0 or newer. (#2347)
- Fixed issue where `NetcodeSettingsProvider` would throw an exception in Unity 2020.3.x versions. (#2345)
Expand Down
11 changes: 10 additions & 1 deletion com.unity.netcode.gameobjects/Components/NetworkAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,16 @@ internal unsafe void UpdateParameters(ref ParametersUpdateMessage parametersUpda
/// </summary>
internal void UpdateAnimationState(AnimationState animationState)
{
// Handle updating layer weights first.
if (animationState.Layer < m_LayerWeights.Length)
{
if (m_LayerWeights[animationState.Layer] != animationState.Weight)
{
m_Animator.SetLayerWeight(animationState.Layer, animationState.Weight);
}
}

// If there is no state transition then return
if (animationState.StateHash == 0)
{
return;
Expand Down Expand Up @@ -1147,7 +1157,6 @@ internal void UpdateAnimationState(AnimationState animationState)
m_Animator.Play(animationState.StateHash, animationState.Layer, animationState.NormalizedTime);
}
}
m_Animator.SetLayerWeight(animationState.Layer, animationState.Weight);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,27 @@ private void BeginAttack(int weaponType)
m_NetworkAnimator.SetTrigger("Attack");
}

private void SetLayerWeight(int layer, float weight)
{
m_Animator.SetLayerWeight(layer, weight);
}

private float GetLayerWeight(int layer)
{
return m_Animator.GetLayerWeight(layer);
}

private void LateUpdate()
{

if (!IsSpawned || !IsOwner)
{
if (!IsOwner && IsSpawned)
{
if (Input.GetKeyDown(KeyCode.Alpha4))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like debug code maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is part of the manual test that just allows you to verify that when changes are applied to a layer's weight that it was updated on other non-authoritative/owner instances.

{
Debug.Log($"Layer 1 weight: {GetLayerWeight(1)}");
}
DisplayTestIntValueIfChanged();
return;
}
Expand Down Expand Up @@ -229,6 +243,10 @@ private void LateUpdate()
BeginAttack(2);
}

if (Input.GetKeyDown(KeyCode.Alpha3))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is part of the manual test that just allows you to verify that when changes are applied to a layer's weight that it was updated on other non-authoritative/owner instances.

{
SetLayerWeight(1, 0.75f);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,15 @@ public NetworkAnimator GetNetworkAnimator()
{
return m_NetworkAnimator;
}

public void SetLayerWeight(int layer, float weight)
{
m_Animator.SetLayerWeight(layer, weight);
}

public float GetLayerWeight(int layer)
{
return m_Animator.GetLayerWeight(layer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ public IEnumerator ParameterUpdateTests([Values] OwnerShipMode ownerShipMode, [V

private bool AllTriggersDetected(OwnerShipMode ownerShipMode)
{
var serverParameters = AnimatorTestHelper.ServerSideInstance.GetParameterValues();
if (ownerShipMode == OwnerShipMode.ClientOwner)
{
if (!TriggerTest.ClientsThatTriggered.Contains(m_ServerNetworkManager.LocalClientId))
Expand All @@ -213,6 +212,31 @@ private bool AllTriggersDetected(OwnerShipMode ownerShipMode)
return true;
}

private bool AllInstancesSameLayerWeight(OwnerShipMode ownerShipMode, int layer, float targetWeight)
{

if (ownerShipMode == OwnerShipMode.ClientOwner)
{
if (AnimatorTestHelper.ServerSideInstance.GetLayerWeight(layer) != targetWeight)
{
return false;
}
}

foreach (var animatorTestHelper in AnimatorTestHelper.ClientSideInstances)
{
if (ownerShipMode == OwnerShipMode.ClientOwner && animatorTestHelper.Value.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)
{
continue;
}
if (animatorTestHelper.Value.GetLayerWeight(layer) != targetWeight)
{
return false;
}
}
return true;
}

private bool WaitForClientsToInitialize()
{
foreach (var networkManager in m_ClientNetworkManagers)
Expand Down Expand Up @@ -367,6 +391,56 @@ protected override void OnNewClientCreated(NetworkManager networkManager)
networkManager.NetworkConfig.Prefabs.Add(networkPrefab);
}

/// <summary>
/// Verifies that triggers are synchronized with currently connected clients
/// </summary>
/// <param name="authoritativeMode">Server or Owner authoritative</param>
[UnityTest]
public IEnumerator WeightUpdateTests([Values] OwnerShipMode ownerShipMode, [Values] AuthoritativeMode authoritativeMode)
{
CheckStateEnterCount.ResetTest();
TriggerTest.ResetTest();
VerboseDebug($" ++++++++++++++++++ Weight Test [{ownerShipMode}] Starting ++++++++++++++++++ ");
TriggerTest.IsVerboseDebug = m_EnableVerboseDebug;
AnimatorTestHelper.IsTriggerTest = m_EnableVerboseDebug;

// Spawn our test animator object
var objectInstance = SpawnPrefab(ownerShipMode == OwnerShipMode.ClientOwner, authoritativeMode);

// Wait for it to spawn server-side
yield return WaitForConditionOrTimeOut(() => AnimatorTestHelper.ServerSideInstance != null);
AssertOnTimeout($"Timed out waiting for the server-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");

// Wait for it to spawn client-side
yield return WaitForConditionOrTimeOut(WaitForClientsToInitialize);
AssertOnTimeout($"Timed out waiting for the client-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
var animatorTestHelper = ownerShipMode == OwnerShipMode.ClientOwner ? AnimatorTestHelper.ClientSideInstances[m_ClientNetworkManagers[0].LocalClientId] : AnimatorTestHelper.ServerSideInstance;
var layerCount = animatorTestHelper.GetAnimator().layerCount;

var animationStateCount = animatorTestHelper.GetAnimatorStateCount();
Assert.True(layerCount == animationStateCount, $"AnimationState count {animationStateCount} does not equal the layer count {layerCount}!");

if (authoritativeMode == AuthoritativeMode.ServerAuth)
{
animatorTestHelper = AnimatorTestHelper.ServerSideInstance;
}

animatorTestHelper.SetLayerWeight(1, 0.75f);
// Wait for all instances to update their weight value for layer 1
yield return WaitForConditionOrTimeOut(() => AllInstancesSameLayerWeight(ownerShipMode, 1, 0.75f));
AssertOnTimeout($"Timed out waiting for all instances to match weight 0.75 on layer 1!");

// Now late join a client
yield return CreateAndStartNewClient();

// Verify the late joined client is synchronized to the changed weight
yield return WaitForConditionOrTimeOut(() => AllInstancesSameLayerWeight(ownerShipMode, 1, 0.75f));
AssertOnTimeout($"[Late-Join] Timed out waiting for all instances to match weight 0.75 on layer 1!");

AnimatorTestHelper.IsTriggerTest = false;
VerboseDebug($" ------------------ Weight Test [{ownerShipMode}] Stopping ------------------ ");
}

/// <summary>
/// Verifies that late joining clients are synchronized to an
/// animator's trigger state.
Expand Down