feat(sverm): the Ravager's ram lunges with the clip's root motion

Vanilla only takes root motion during a native attack, which this form never
starts, so a relay on the costume's Animator collects it and a prefix on
Character.ApplyRootMotion applies it to the player the same way (x55).
This commit is contained in:
kanym 2026-10-17 20:24:43 +02:00
commit 63e252d9e9

View file

@ -500,6 +500,37 @@ namespace RacesOfValheim
}
}
/// <summary>The ram clip's own forward motion, collected by RavagerRootMotionRelay and
/// spent by Sverm_RavagerRootMotion_Patch.</summary>
public static Vector3 PendingRootMotion;
/// <summary>
/// Moves the player by the ram clip's root motion, the way vanilla moves a charging Brute.
/// Vanilla's own path (CharacterAnimEvent.OnAnimatorMove into Character.AddRootMotion) only
/// takes motion while Character.InAttack(), and this form never starts a native attack, so
/// the motion comes in here instead and is applied exactly as Character.ApplyRootMotion
/// applies it: delta times 55, used when it is faster than the walk velocity.
/// </summary>
[HarmonyPatch(typeof(Character), "ApplyRootMotion")]
public static class Sverm_RavagerRootMotion_Patch
{
public static void Prefix(Character __instance, ref Vector3 vel)
{
if (__instance != Player.m_localPlayer) return;
Vector3 motion = PendingRootMotion;
PendingRootMotion = Vector3.zero;
if (!IsActive || !LocalAttacking) return;
Vector3 push = motion * 55f;
push.y = 0f;
if (push.magnitude > new Vector3(vel.x, 0f, vel.z).magnitude)
{
vel = new Vector3(push.x, vel.y, push.z);
}
}
}
[HarmonyPatch(typeof(Humanoid), "StartAttack")]
public static class Sverm_RavagerBlockNativeAttack_Patch
{
@ -546,6 +577,23 @@ namespace RacesOfValheim
}
}
/// <summary>
/// Sits on the costume's Animator object. Unity calls OnAnimatorMove there instead of moving
/// the costume itself; while Active (the ram), the clip's motion is handed to the player
/// through SvermRavagerForm.PendingRootMotion. Otherwise it is dropped, so the costume stays
/// where the player is.
/// </summary>
public class RavagerRootMotionRelay : MonoBehaviour
{
public Animator Animator;
public bool Active;
private void OnAnimatorMove()
{
if (Active && Animator != null) SvermRavagerForm.PendingRootMotion += Animator.deltaPosition;
}
}
/// <summary>
/// Locomotion AND combat, both held on every frame while the form is up - built on the same
/// method FenrirFormBehavior and DrakeFormBehavior already use for their own transformations,
@ -590,6 +638,8 @@ namespace RacesOfValheim
private Transform leftClaw;
private Transform rightClaw;
private RavagerRootMotionRelay rootMotion;
private void Start()
{
foreach (Transform bone in GetComponentsInChildren<Transform>(true))
@ -615,6 +665,13 @@ namespace RacesOfValheim
anim.fireEvents = false;
anim.cullingMode = AnimatorCullingMode.AlwaysAnimate;
// The ram is the exception: its clip's motion moves the PLAYER (see
// Sverm_RavagerRootMotion_Patch). With an OnAnimatorMove receiver on the Animator's
// object, Unity computes the motion and hands it over instead of moving the costume.
rootMotion = anim.gameObject.AddComponent<RavagerRootMotionRelay>();
rootMotion.Animator = anim;
anim.applyRootMotion = true;
foreach (AnimatorControllerParameter param in anim.parameters)
{
if (param.type == AnimatorControllerParameterType.Float && param.name == "forward_speed")
@ -696,15 +753,15 @@ namespace RacesOfValheim
// be the only reliable option.
if (RaceInput.AttackDown(1))
{
StartCoroutine(AttackRoutine(SvermRavagerAttacks.Ram, "Attack Ram", 2f, false));
StartCoroutine(AttackRoutine(SvermRavagerAttacks.Ram, "Attack Ram", 2f, false, true));
}
else if (RaceInput.AttackDown(0))
{
StartCoroutine(AttackRoutine(SvermRavagerAttacks.Bite, "Attack Bite", 2f, false));
StartCoroutine(AttackRoutine(SvermRavagerAttacks.Bite, "Attack Bite", 2f, false, false));
}
else if (RaceInput.AttackDown(2))
{
StartCoroutine(AttackRoutine(SvermRavagerAttacks.Slam, "Attack Slam", 1.2f, true));
StartCoroutine(AttackRoutine(SvermRavagerAttacks.Slam, "Attack Slam", 1.2f, true, false));
}
else if ((SvermConfigManagement.RavagerKey.Value.IsDown() ||
RaceInput.ConsumePress(SvermConfigManagement.RavagerKey.Value)) && Time.time >= nextTauntAt)
@ -719,13 +776,14 @@ namespace RacesOfValheim
/// event in the clip is a hit here, at the same point of the clip (the bite and the ram
/// have one, the slam has two, one per claw). The player is held still until the
/// animator starts blending out of the attack state, which is when the real Brute can
/// move again.
/// move again. With lunge, the clip's own root motion carries the player forward (the ram).
/// </summary>
private System.Collections.IEnumerator AttackRoutine(SvermRavagerAttacks.Profile profile, string stateName,
float radius, bool slam)
float radius, bool slam, bool lunge)
{
isAttacking = true;
bool played = StartAttackState(profile);
if (rootMotion != null) rootMotion.Active = lunge;
int dropSamples = 0;
float[] leftY = new float[ClawHistory];
@ -743,6 +801,7 @@ namespace RacesOfValheim
dropSamples++;
});
if (rootMotion != null) rootMotion.Active = false;
isAttacking = false;
}