Skip to content

Commit c5db1e3

Browse files
committed
fix: Resolving Sync Issues and adding ResetTrigger support (#1327)
* fix: Adding the ability to call ResetTrigger for a NetworkAnimator. * chore: fix out of sync values when no value changes where happening.
1 parent d0d9cce commit c5db1e3

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66

77
Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).
88

9+
## [1.0.0-pre.3] - 2021-10-22
10+
11+
### Added
12+
13+
- ResetTrigger function to NetworkAnimator
14+
15+
### Fixed
16+
17+
- Overflow exception when syncing Animator state.
18+
919
## [1.0.0-pre.2] - 2020-12-20
1020

1121
### Added

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

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Unity.Collections;
22
using Unity.Collections.LowLevel.Unsafe;
3+
34
using UnityEngine;
45

56
namespace Unity.Netcode.Components
@@ -38,11 +39,12 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
3839
internal struct AnimationTriggerMessage : INetworkSerializable
3940
{
4041
public int Hash;
42+
public bool Reset;
4143

4244
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
4345
{
4446
serializer.SerializeValue(ref Hash);
45-
47+
serializer.SerializeValue(ref Reset);
4648
}
4749
}
4850

@@ -286,6 +288,12 @@ private bool CheckAnimStateChanged(out int stateHash, out float normalizedTime)
286288
return false;
287289
}
288290

291+
/* $AS TODO: Right now we are not checking for changed values this is because
292+
the read side of this function doesn't have similar logic which would cause
293+
an overflow read because it doesn't know if the value is there or not. So
294+
there needs to be logic to track which indexes changed in order for there
295+
to be proper value change checking. Will revist in 1.1.0.
296+
*/
289297
private unsafe bool WriteParameters(FastBufferWriter writer, bool autoSend)
290298
{
291299
if (m_CachedAnimatorParameters == null)
@@ -308,39 +316,27 @@ private unsafe bool WriteParameters(FastBufferWriter writer, bool autoSend)
308316
var valueInt = m_Animator.GetInteger(hash);
309317
fixed (void* value = cacheValue.Value)
310318
{
311-
var oldValue = UnsafeUtility.AsRef<int>(value);
312-
if (valueInt != oldValue)
313-
{
314-
UnsafeUtility.WriteArrayElement(value, 0, valueInt);
315-
BytePacker.WriteValuePacked(writer, (uint)valueInt);
316-
}
319+
UnsafeUtility.WriteArrayElement(value, 0, valueInt);
320+
BytePacker.WriteValuePacked(writer, (uint)valueInt);
317321
}
318322
}
319323
else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterBool)
320324
{
321325
var valueBool = m_Animator.GetBool(hash);
322326
fixed (void* value = cacheValue.Value)
323327
{
324-
var oldValue = UnsafeUtility.AsRef<bool>(value);
325-
if (valueBool != oldValue)
326-
{
327-
UnsafeUtility.WriteArrayElement(value, 0, valueBool);
328-
writer.WriteValueSafe(valueBool);
329-
}
328+
UnsafeUtility.WriteArrayElement(value, 0, valueBool);
329+
writer.WriteValueSafe(valueBool);
330330
}
331331
}
332332
else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterFloat)
333333
{
334334
var valueFloat = m_Animator.GetFloat(hash);
335335
fixed (void* value = cacheValue.Value)
336336
{
337-
var oldValue = UnsafeUtility.AsRef<float>(value);
338-
if (valueFloat != oldValue)
339-
{
340-
UnsafeUtility.WriteArrayElement(value, 0, valueFloat);
341337

342-
writer.WriteValueSafe(valueFloat);
343-
}
338+
UnsafeUtility.WriteArrayElement(value, 0, valueFloat);
339+
writer.WriteValueSafe(valueFloat);
344340
}
345341
}
346342
}
@@ -432,23 +428,41 @@ private unsafe void SendAnimStateClientRpc(AnimationMessage animSnapshot, Client
432428
[ClientRpc]
433429
private void SendAnimTriggerClientRpc(AnimationTriggerMessage animSnapshot, ClientRpcParams clientRpcParams = default)
434430
{
435-
m_Animator.SetTrigger(animSnapshot.Hash);
431+
if (animSnapshot.Reset)
432+
{
433+
m_Animator.ResetTrigger(animSnapshot.Hash);
434+
}
435+
else
436+
{
437+
m_Animator.SetTrigger(animSnapshot.Hash);
438+
}
436439
}
437440

438441
public void SetTrigger(string triggerName)
439442
{
440443
SetTrigger(Animator.StringToHash(triggerName));
441444
}
442445

443-
public void SetTrigger(int hash)
446+
public void SetTrigger(int hash, bool reset = false)
444447
{
445448
var animMsg = new AnimationTriggerMessage();
446449
animMsg.Hash = hash;
450+
animMsg.Reset = reset;
447451

448452
if (IsServer)
449453
{
450454
SendAnimTriggerClientRpc(animMsg);
451455
}
452456
}
457+
458+
public void ResetTrigger(string triggerName)
459+
{
460+
ResetTrigger(Animator.StringToHash(triggerName));
461+
}
462+
463+
public void ResetTrigger(int hash)
464+
{
465+
SetTrigger(hash, true);
466+
}
453467
}
454468
}

0 commit comments

Comments
 (0)