Skip to content

Commit d07cd7f

Browse files
fix: match NetworkAnimator transition info by originating state only
Transitions not in the table no longer fall back to the first entry for the destination, and a missing sub-state machine reference no longer throws while the table is built.
1 parent a15c9fb commit d07cd7f

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ private void BuildDestinationToTransitionInfoTable()
300300
}
301301

302302
/// <summary>
303-
/// Gets the transition info for a destination state, preferring the entry that starts from the originating state
304-
/// since several states can transition into the same destination.
303+
/// Gets the transition info for a destination state, matched by originating state since several states can
304+
/// transition into the same destination. Transitions not in the table, such as non-trigger ones, return false.
305305
/// </summary>
306306
internal bool TryGetTransitionInfo(int layer, int destinationState, int originatingState, out TransitionStateInfo transitionInfo)
307307
{
@@ -311,16 +311,15 @@ internal bool TryGetTransitionInfo(int layer, int destinationState, int originat
311311
return false;
312312
}
313313

314-
transitionInfo = transitions[0];
315314
foreach (var entry in transitions)
316315
{
317316
if (entry.OriginatingState == originatingState)
318317
{
319318
transitionInfo = entry;
320-
break;
319+
return true;
321320
}
322321
}
323-
return true;
322+
return false;
324323
}
325324

326325
[Serializable]
@@ -493,7 +492,11 @@ private void ParseStateMachineStates(int layerIndex, ref AnimatorController anim
493492
foreach (var childStateMachine in stateMachine.stateMachines)
494493
{
495494
var nestedStateMachine = childStateMachine.stateMachine;
496-
ParseStateMachineStates(layerIndex, ref animatorController, ref nestedStateMachine, childStateMachineParents);
495+
// A missing sub-state machine asset reference leaves this null
496+
if (nestedStateMachine != null)
497+
{
498+
ParseStateMachineStates(layerIndex, ref animatorController, ref nestedStateMachine, childStateMachineParents);
499+
}
497500
}
498501
}
499502

@@ -1104,14 +1107,11 @@ private void WriteSynchronizationData<T>(ref BufferSerializer<T> serializer) whe
11041107
stateHash = nextState.fullPathHash;
11051108

11061109
// Check if this transition can be synchronized using cross-fading
1107-
if (m_DestinationStateToTransitionInfo.ContainsKey(layer))
1110+
if (TryGetTransitionInfo(layer, nextState.shortNameHash, synchronizationStateInfo.shortNameHash, out var transitionInfo))
11081111
{
1109-
if (TryGetTransitionInfo(layer, nextState.shortNameHash, synchronizationStateInfo.shortNameHash, out var transitionInfo))
1110-
{
1111-
stateHash = transitionInfo.OriginatingState;
1112-
// Set the destination state to cross-fade to from the originating state
1113-
animationState.DestinationStateHash = transitionInfo.DestinationState;
1114-
}
1112+
stateHash = transitionInfo.OriginatingState;
1113+
// Set the destination state to cross-fade to from the originating state
1114+
animationState.DestinationStateHash = transitionInfo.DestinationState;
11151115
}
11161116
}
11171117

‎com.unity.netcode.gameobjects/Tests/Editor/NetworkAnimatorEditorTests.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ public void TransitionInfoLookupMatchesOriginatingState()
239239
Assert.That(networkAnimator.TryGetTransitionInfo(0, stateB.nameHash, stateC.nameHash, out var infoFromC), Is.True);
240240
Assert.That(infoFromC.OriginatingState, Is.EqualTo(stateC.nameHash));
241241
Assert.That(infoFromC.TransitionDuration, Is.EqualTo(transitionFromC.duration));
242+
Assert.That(networkAnimator.TryGetTransitionInfo(0, stateB.nameHash, stateB.nameHash, out _), Is.False);
242243
}
243244

244245
private TestNetworkAnimator CreateNetworkAnimator(AnimatorController controller, string name)

0 commit comments

Comments
 (0)