Skip to content

Commit 0f4109b

Browse files
fix: server doesn't apply triggers to itself; add host and server Animation tests (#1746) (#1751)
* fix: remove auto-send animation parameters (#1746) * comment updates
1 parent b2129fe commit 0f4109b

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ private unsafe void ReadParameters(FastBufferReader reader)
300300
}
301301
}
302302

303+
/// <summary>
304+
/// Internally-called RPC client receiving function to update some animation parameters on a client when
305+
/// the server wants to update them
306+
/// </summary>
307+
/// <param name="animSnapshot">the payload containing the parameters to apply</param>
308+
/// <param name="clientRpcParams">unused</param>
303309
[ClientRpc]
304310
private unsafe void SendAnimStateClientRpc(AnimationMessage animSnapshot, ClientRpcParams clientRpcParams = default)
305311
{
@@ -320,6 +326,12 @@ private unsafe void SendAnimStateClientRpc(AnimationMessage animSnapshot, Client
320326
}
321327
}
322328

329+
/// <summary>
330+
/// Internally-called RPC client receiving function to update a trigger when the server wants to forward
331+
/// a trigger for a client to play / reset
332+
/// </summary>
333+
/// <param name="animSnapshot">the payload containing the trigger data to apply</param>
334+
/// <param name="clientRpcParams">unused</param>
323335
[ClientRpc]
324336
private void SendAnimTriggerClientRpc(AnimationTriggerMessage animSnapshot, ClientRpcParams clientRpcParams = default)
325337
{
@@ -359,8 +371,23 @@ public void SetTrigger(int hash, bool reset = false)
359371

360372
if (IsServer)
361373
{
374+
// trigger the animation locally on the server...
375+
if (reset)
376+
{
377+
m_Animator.ResetTrigger(hash);
378+
}
379+
else
380+
{
381+
m_Animator.SetTrigger(hash);
382+
}
383+
384+
// ...then tell all the clients to do the same
362385
SendAnimTriggerClientRpc(animMsg);
363386
}
387+
else
388+
{
389+
Debug.LogWarning("Trying to call NetworkAnimator.SetTrigger on a client...ignoring");
390+
}
364391
}
365392

366393
/// <summary>

com.unity.netcode.gameobjects/Tests/Runtime/NetworkAnimator/NetworkAnimatorTests.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,21 @@ public class NetworkAnimatorTests : BaseMultiInstanceTest
1818
private Animator m_PlayerOnServerAnimator;
1919
private Animator m_PlayerOnClientAnimator;
2020

21+
2122
[UnitySetUp]
2223
public override IEnumerator Setup()
2324
{
24-
yield return StartSomeClientsAndServerWithPlayers(useHost: true, nbClients: NbClients,
25+
// this is treacherous...normally BaseMultiInstance calls StartSomeClientsAndServerWithPlayers for you
26+
// in its version of Setup. In this case, I want to alternatively test Server and Host mode via parameters
27+
// in each test. However I cannot inject the host / server mode into the corresponding Setup calls, hence
28+
// I wrote and manually call an Init function. However, if I don't write this null version of setup
29+
// I will get 2 calls to StartSomeClientsAndServerWithPlayers, which wrecks everything.
30+
yield return null;
31+
}
32+
33+
public IEnumerator Init(bool hostMode)
34+
{
35+
yield return StartSomeClientsAndServerWithPlayers(useHost: hostMode, nbClients: NbClients,
2536
updatePlayerPrefab: playerPrefab =>
2637
{
2738
// ideally, we would build up the AnimatorController entirely in code and not need an asset,
@@ -63,8 +74,10 @@ private bool HasClip(Animator animator, string clipName)
6374
}
6475

6576
[UnityTest]
66-
public IEnumerator AnimationTriggerReset([Values(true, false)] bool asHash)
77+
public IEnumerator AnimationTriggerReset([Values(true, false)] bool asHash, [Values(true, false)] bool hostMode)
6778
{
79+
yield return Init(hostMode);
80+
6881
// We have "UnboundTrigger" purposely not bound to any animations so we can test resetting.
6982
// If we used a trigger that was bound to a transition, then the trigger would reset as soon as the
7083
// transition happens. This way it will stay stuck on
@@ -114,9 +127,12 @@ public IEnumerator AnimationTriggerReset([Values(true, false)] bool asHash)
114127
Assert.False(s_GloabalTimeOutHelper.TimedOut, "Timed out on client reset check");
115128
}
116129

130+
117131
[UnityTest]
118-
public IEnumerator AnimationStateSyncTest()
132+
public IEnumerator AnimationStateSyncTest([Values(true, false)] bool hostMode)
119133
{
134+
yield return Init(hostMode);
135+
120136
// check that we have started in the default state
121137
Assert.True(m_PlayerOnServerAnimator.GetCurrentAnimatorStateInfo(0).IsName("DefaultState"));
122138
Assert.True(m_PlayerOnClientAnimator.GetCurrentAnimatorStateInfo(0).IsName("DefaultState"));
@@ -137,8 +153,10 @@ public IEnumerator AnimationStateSyncTest()
137153
}
138154

139155
[UnityTest]
140-
public IEnumerator AnimationStateSyncTriggerTest([Values(true, false)] bool asHash)
156+
public IEnumerator AnimationStateSyncTriggerTest([Values(true, false)] bool asHash, [Values(true, false)] bool hostMode)
141157
{
158+
yield return Init(hostMode);
159+
142160
string triggerString = "TestTrigger";
143161
int triggerHash = Animator.StringToHash(triggerString);
144162

@@ -172,8 +190,9 @@ public IEnumerator AnimationStateSyncTriggerTest([Values(true, false)] bool asHa
172190
}
173191

174192
[UnityTest]
175-
public IEnumerator AnimationStateSyncTestWithOverride()
193+
public IEnumerator AnimationStateSyncTestWithOverride([Values(true, false)] bool hostMode)
176194
{
195+
yield return Init(hostMode);
177196
// set up the animation override controller
178197
var overrideController = Resources.Load("TestAnimatorOverrideController") as AnimatorOverrideController;
179198
m_PlayerOnServer.GetComponent<Animator>().runtimeAnimatorController = overrideController;

com.unity.netcode.gameobjects/Tests/Runtime/NetworkAnimator/Resources/TestAnimatorController.controller

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,19 @@ AnimatorController:
147147
m_DefaultFloat: 0
148148
m_DefaultInt: 0
149149
m_DefaultBool: 0
150-
m_Controller: {fileID: 0}
150+
m_Controller: {fileID: 9100000}
151151
- m_Name: TestTrigger
152152
m_Type: 9
153153
m_DefaultFloat: 0
154154
m_DefaultInt: 0
155155
m_DefaultBool: 0
156-
m_Controller: {fileID: 0}
156+
m_Controller: {fileID: 9100000}
157157
- m_Name: UnboundTrigger
158158
m_Type: 9
159159
m_DefaultFloat: 0
160160
m_DefaultInt: 0
161161
m_DefaultBool: 0
162-
m_Controller: {fileID: 0}
162+
m_Controller: {fileID: 9100000}
163163
m_AnimatorLayers:
164164
- serializedVersion: 5
165165
m_Name: Base Layer

0 commit comments

Comments
 (0)