Skip to content

Commit 4fe966c

Browse files
fix: late-joining client cross-fades from the originating state
CrossFade normalizes the transition duration to the current state's length, so cross-fading from the default state blended at the wrong speed when the clip lengths differ.
1 parent d07cd7f commit 4fe966c

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,10 +1572,16 @@ private void UpdateAnimationState(AnimationState animationState)
15721572
{
15731573
if (TryGetTransitionInfo(animationState.Layer, animationState.DestinationStateHash, animationState.StateHash, out var transitionInfo))
15741574
{
1575-
// A late-joining client starts in its default state, which may not be the originating state,
1576-
// so it cross-fades from whatever state it is in unless it is already mid-transition
1577-
if (currentState.shortNameHash == animationState.StateHash || !m_Animator.IsInTransition(animationState.Layer))
1575+
var onOriginatingState = currentState.shortNameHash == animationState.StateHash;
1576+
if (onOriginatingState || !m_Animator.IsInTransition(animationState.Layer))
15781577
{
1578+
// A late-joining client starts in its default state. CrossFade normalizes the duration to the
1579+
// current state's length, so enter the originating state first and apply it before cross-fading.
1580+
if (!onOriginatingState)
1581+
{
1582+
m_Animator.Play(animationState.StateHash, animationState.Layer, 0.0f);
1583+
m_Animator.Update(0.0f);
1584+
}
15791585
// Cross-fade from the current to the destination state for the transitions duration while starting at the server's current normalized time of the transition
15801586
m_Animator.CrossFade(transitionInfo.DestinationState, transitionInfo.TransitionDuration, transitionInfo.Layer, 0.0f, animationState.NormalizedTime);
15811587
}

‎com.unity.netcode.gameobjects/Tests/Runtime/Components/NetworkAnimatorLateJoinTransitionTests.cs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#if COM_UNITY_MODULES_ANIMATION && UNITY_EDITOR
22
using System.Collections;
33
using System.Text;
4+
using NUnit.Framework;
45
using Unity.Netcode.Components;
56
using Unity.Netcode.TestHelpers.Runtime;
67
using UnityEditor;
@@ -38,12 +39,18 @@ protected override void OnServerAndClientsCreated()
3839
m_DestinationState = rootStateMachine.AddState("State B");
3940
rootStateMachine.defaultState = m_DefaultState;
4041

42+
// The source clip is much longer than the default one, so a cross-fade normalized to the wrong state blends far faster
43+
m_DefaultState.motion = CreateClip("Short", 1.0f);
44+
m_SourceState.motion = CreateClip("Long", 100.0f);
45+
m_DestinationState.motion = CreateClip("Destination", 1.0f);
46+
4147
// Both states transition into the same destination, and the transitions are long enough to still be running when the client joins
4248
foreach (var state in new[] { m_DefaultState, m_SourceState })
4349
{
4450
var transition = state.AddTransition(m_DestinationState);
4551
transition.hasExitTime = false;
46-
transition.duration = 10.0f;
52+
transition.hasFixedDuration = false;
53+
transition.duration = 0.5f;
4754
transition.AddCondition(AnimatorConditionMode.If, 0.0f, k_TriggerName);
4855
}
4956
AssetDatabase.SaveAssets();
@@ -91,6 +98,15 @@ public IEnumerator LateJoinFromNonDefaultStateCrossFadesToDestination()
9198

9299
yield return WaitForConditionOrTimeOut(errorLog => ReachingDestination(clientAnimator, errorLog));
93100
AssertOnTimeout($"Late-joining client did not cross-fade to {m_DestinationState.name}!");
101+
Assert.That(clientAnimator.GetCurrentAnimatorStateInfo(0).shortNameHash, Is.EqualTo(m_SourceState.nameHash), $"Late-joining client did not cross-fade from {m_SourceState.name}!");
102+
}
103+
104+
private AnimationClip CreateClip(string name, float length)
105+
{
106+
var clip = new AnimationClip { name = name };
107+
clip.SetCurve(string.Empty, typeof(Transform), "localScale.x", AnimationCurve.Linear(0.0f, 1.0f, length, 1.0f));
108+
AssetDatabase.AddObjectToAsset(clip, m_Controller);
109+
return clip;
94110
}
95111

96112
private bool ReachingDestination(Animator animator, StringBuilder errorLog)

0 commit comments

Comments
 (0)