Skip to content

fix: networkanimator only updates observers #3057

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
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

### Fixed

- Fixed issue where `NetworkAnimator` would send updates to non-observer clients. (#3057)
- Fixed issue where an exception could occur when receiving a universal RPC for a `NetworkObject` that has been despawned. (#3052)
- Fixed issue where a NetworkObject hidden from a client that is then promoted to be session owner was not being synchronized with newly joining clients.(#3051)
- Fixed issue where clients could have a wrong time delta on `NetworkVariableBase` which could prevent from sending delta state updates. (#3045)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -952,8 +952,14 @@ internal void CheckForAnimatorChanges()
{
// Just notify all remote clients and not the local server
m_ClientSendList.Clear();
m_ClientSendList.AddRange(NetworkManager.ConnectedClientsIds);
m_ClientSendList.Remove(NetworkManager.LocalClientId);
foreach (var clientId in NetworkManager.ConnectedClientsIds)
{
if (clientId == NetworkManager.LocalClientId || !NetworkObject.Observers.Contains(clientId))
{
continue;
}
m_ClientSendList.Add(clientId);
}
m_ClientRpcParams.Send.TargetClientIds = m_ClientSendList;
SendAnimStateClientRpc(m_AnimationMessage, m_ClientRpcParams);
}
Expand Down Expand Up @@ -1264,9 +1270,15 @@ private unsafe void SendParametersUpdateServerRpc(ParametersUpdateMessage parame
if (NetworkManager.ConnectedClientsIds.Count > (IsHost ? 2 : 1))
{
m_ClientSendList.Clear();
m_ClientSendList.AddRange(NetworkManager.ConnectedClientsIds);
m_ClientSendList.Remove(serverRpcParams.Receive.SenderClientId);
m_ClientSendList.Remove(NetworkManager.ServerClientId);
foreach (var clientId in NetworkManager.ConnectedClientsIds)
{
if (clientId == serverRpcParams.Receive.SenderClientId || clientId == NetworkManager.ServerClientId || !NetworkObject.Observers.Contains(clientId))
{
continue;
}
m_ClientSendList.Add(clientId);
}

m_ClientRpcParams.Send.TargetClientIds = m_ClientSendList;
m_NetworkAnimatorStateChangeHandler.SendParameterUpdate(parametersUpdate, m_ClientRpcParams);
}
Expand Down Expand Up @@ -1321,9 +1333,14 @@ private void SendAnimStateServerRpc(AnimationMessage animationMessage, ServerRpc
if (NetworkManager.ConnectedClientsIds.Count > (IsHost ? 2 : 1))
{
m_ClientSendList.Clear();
m_ClientSendList.AddRange(NetworkManager.ConnectedClientsIds);
m_ClientSendList.Remove(serverRpcParams.Receive.SenderClientId);
m_ClientSendList.Remove(NetworkManager.ServerClientId);
foreach (var clientId in NetworkManager.ConnectedClientsIds)
{
if (clientId == serverRpcParams.Receive.SenderClientId || clientId == NetworkManager.ServerClientId || !NetworkObject.Observers.Contains(clientId))
{
continue;
}
m_ClientSendList.Add(clientId);
}
m_ClientRpcParams.Send.TargetClientIds = m_ClientSendList;
m_NetworkAnimatorStateChangeHandler.SendAnimationUpdate(animationMessage, m_ClientRpcParams);
}
Expand Down Expand Up @@ -1390,9 +1407,14 @@ internal void SendAnimTriggerServerRpc(AnimationTriggerMessage animationTriggerM
InternalSetTrigger(animationTriggerMessage.Hash, animationTriggerMessage.IsTriggerSet);

m_ClientSendList.Clear();
m_ClientSendList.AddRange(NetworkManager.ConnectedClientsIds);
m_ClientSendList.Remove(NetworkManager.ServerClientId);

foreach (var clientId in NetworkManager.ConnectedClientsIds)
{
if (clientId == NetworkManager.ServerClientId || !NetworkObject.Observers.Contains(clientId))
{
continue;
}
m_ClientSendList.Add(clientId);
}
if (IsServerAuthoritative())
{
m_NetworkAnimatorStateChangeHandler.QueueTriggerUpdateToClient(animationTriggerMessage, m_ClientRpcParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,103 @@ public IEnumerator CrossFadeTransitionTests()
AssertOnTimeout($"Timed out waiting for all clients to transition from synchronized cross fade!");
}

private bool AllTriggersDetectedOnObserversOnly(OwnerShipMode ownerShipMode, ulong nonObserverId)
{
if (ownerShipMode == OwnerShipMode.ClientOwner)
{
if (!TriggerTest.ClientsThatTriggered.Contains(m_ServerNetworkManager.LocalClientId))
{
return false;
}
}

foreach (var animatorTestHelper in AnimatorTestHelper.ClientSideInstances)
{
var currentClientId = animatorTestHelper.Value.NetworkManager.LocalClientId;
if (currentClientId == nonObserverId || (ownerShipMode == OwnerShipMode.ClientOwner && currentClientId == animatorTestHelper.Value.OwnerClientId))
{
continue;
}

if (!TriggerTest.ClientsThatTriggered.Contains(currentClientId))
{
return false;
}
}

// Should return false always
return !TriggerTest.ClientsThatTriggered.Contains(nonObserverId);
}

private bool AllObserversSameLayerWeight(OwnerShipMode ownerShipMode, int layer, float targetWeight, ulong nonObserverId)
{

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

foreach (var animatorTestHelper in AnimatorTestHelper.ClientSideInstances)
{
var currentClientId = animatorTestHelper.Value.NetworkManager.LocalClientId;
if (ownerShipMode == OwnerShipMode.ClientOwner && animatorTestHelper.Value.OwnerClientId == currentClientId)
{
continue;
}
if (currentClientId == nonObserverId)
{
if (animatorTestHelper.Value.GetLayerWeight(layer) == targetWeight)
{
return false;
}
}
else
if (animatorTestHelper.Value.GetLayerWeight(layer) != targetWeight)
{
return false;
}
}
return true;
}

[UnityTest]
public IEnumerator OnlyObserversAnimateTest()
{
// Spawn our test animator object
var objectInstance = SpawnPrefab(m_OwnerShipMode == OwnerShipMode.ClientOwner, m_AuthoritativeMode);
var networkObject = objectInstance.GetComponent<NetworkObject>();
// Wait for it to spawn server-side
var success = WaitForConditionOrTimeOutWithTimeTravel(() => AnimatorTestHelper.ServerSideInstance != null);
Assert.True(success, $"Timed out waiting for the server-side instance of {GetNetworkAnimatorName(m_AuthoritativeMode)} to be spawned!");

// Wait for it to spawn client-side
success = WaitForConditionOrTimeOutWithTimeTravel(WaitForClientsToInitialize);
Assert.True(success, $"Timed out waiting for the server-side instance of {GetNetworkAnimatorName(m_AuthoritativeMode)} to be spawned!");

var animatorTestHelper = m_OwnerShipMode == OwnerShipMode.ClientOwner ? AnimatorTestHelper.ClientSideInstances[m_ClientNetworkManagers[0].LocalClientId] : AnimatorTestHelper.ServerSideInstance;

networkObject.NetworkHide(m_ClientNetworkManagers[1].LocalClientId);

yield return WaitForConditionOrTimeOut(() => !m_ClientNetworkManagers[1].SpawnManager.SpawnedObjects.ContainsKey(networkObject.NetworkObjectId));
AssertOnTimeout($"Client-{m_ClientNetworkManagers[1].LocalClientId} timed out waiting to hide {networkObject.name}!");

if (m_AuthoritativeMode == AuthoritativeMode.ServerAuth)
{
animatorTestHelper = AnimatorTestHelper.ServerSideInstance;
}
animatorTestHelper.SetTrigger();
// Wait for all triggers to fire
yield return WaitForConditionOrTimeOut(() => AllTriggersDetectedOnObserversOnly(m_OwnerShipMode, m_ClientNetworkManagers[1].LocalClientId));
AssertOnTimeout($"Timed out waiting for all triggers to match!");

animatorTestHelper.SetLayerWeight(1, 0.75f);
// Wait for all instances to update their weight value for layer 1
success = WaitForConditionOrTimeOutWithTimeTravel(() => AllObserversSameLayerWeight(m_OwnerShipMode, 1, 0.75f, m_ClientNetworkManagers[1].LocalClientId));
Assert.True(success, $"Timed out waiting for all instances to match weight 0.75 on layer 1!");
}

/// <summary>
/// Verifies that triggers are synchronized with currently connected clients
Expand Down
Loading