Enemy hit-reacts: React/Stagger anim tiers off Health-drop edges (the Rukhanka-safe flinch)

SwordCombat React (1.4x, 0.3s pulse) + Stagger (1.2x, 0.55s) as Any-State
states on AC_EnemyTopDown via EnemyRigTools.WireEnemyHitReacts; driven by
EnemyAnimationDriveSystem's cache (now Pos/Hp/ReactUntil/Heavy) off
replicated Health drops. Windup-honesty gate: light reacts require
!IsAttacking (a sub-poise hit never visually cancels a live windup);
the heavy tier tracks the server's B2 poise break (stagger threshold 50
= finisher staggers, light flinches at the locked tuning). Knobs:
HitReactSeconds (0=off) / HitStaggerSeconds / HitReactStaggerDamage.
Replaces the review-cut positional vibrate (Rukhanka inverse-fold no-op).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-21 00:28:11 -07:00
parent 4e795a38e9
commit 0ff11fe2a9
4 changed files with 325 additions and 8 deletions
@@ -42,13 +42,24 @@ namespace ProjectM.Client
static readonly FastAnimatorParameter k_Speed = new FastAnimatorParameter("Speed");
static readonly FastAnimatorParameter k_IsAttacking = new FastAnimatorParameter("IsAttacking");
static readonly FastAnimatorParameter k_IsDead = new FastAnimatorParameter("IsDead"); // B3: the controller's Death state existed but was never driven
static readonly FastAnimatorParameter k_IsHit = new FastAnimatorParameter("IsHit"); // 07-21 hit-react (G5): pulsed off replicated Health-drop edges
static readonly FastAnimatorParameter k_IsHitHeavy = new FastAnimatorParameter("IsHitHeavy"); // heavy tier -> HitStagger (aligns with the server's B2 poise break)
// prevPos cache (per Husk Entity). Pruned every frame (a vanished Husk = a server-authoritative death).
NativeParallelHashMap<Entity, float3> _prevPos;
/// <summary>07-21: per-Husk anim cache -- position (velocity derivation), Health (the hit-react damage
/// edge), and the active flinch pulse. Pruned every frame (a vanished Husk = a server-authoritative death).</summary>
struct EnemyAnimCache
{
public float3 Pos;
public float Hp;
public float ReactUntil; // wall-clock ElapsedTime the flinch pulse ends (0 = none)
public bool Heavy; // this pulse is the stagger tier
}
NativeParallelHashMap<Entity, EnemyAnimCache> _prevPos;
protected override void OnCreate()
{
_prevPos = new NativeParallelHashMap<Entity, float3>(64, Allocator.Persistent);
_prevPos = new NativeParallelHashMap<Entity, EnemyAnimCache>(64, Allocator.Persistent);
}
protected override void OnDestroy()
@@ -65,6 +76,12 @@ namespace ProjectM.Client
var job = new EnemyDriveJob
{
moveX = k_MoveX, moveZ = k_MoveZ, speed = k_Speed, isAttacking = k_IsAttacking, isDead = k_IsDead,
isHit = k_IsHit, isHitHeavy = k_IsHitHeavy,
// 07-21 hit-react knobs (read main-thread, passed by value -- never FeelConfig inside Burst)
el = (float)SystemAPI.Time.ElapsedTime,
reactSeconds = math.max(0f, FeelConfig.HitReactSeconds),
staggerSeconds = math.max(0f, FeelConfig.HitStaggerSeconds),
staggerDamage = math.max(1f, FeelConfig.HitReactStaggerDamage),
dt = dt,
prevPos = _prevPos,
seen = seen,
@@ -92,11 +109,12 @@ namespace ProjectM.Client
[WithAll(typeof(EnemyTag))]
partial struct EnemyDriveJob : IJobEntity
{
public FastAnimatorParameter moveX, moveZ, speed, isAttacking, isDead;
public FastAnimatorParameter moveX, moveZ, speed, isAttacking, isDead, isHit, isHitHeavy;
public float el, reactSeconds, staggerSeconds, staggerDamage; // 07-21 hit-react (reactSeconds 0 = off)
[Unity.Collections.ReadOnly] public ComponentLookup<IsLunging> isLunging; // A7: a lunge has no AttackWindup; OR it in so the boss/Charger attack anim plays during the committed lunge
public float dt;
public NativeParallelHashMap<Entity, float3> prevPos;
public NativeParallelHashMap<Entity, EnemyAnimCache> prevPos;
public NativeParallelHashSet<Entity> seen;
void Execute(
@@ -111,9 +129,23 @@ namespace ProjectM.Client
seen.Add(e);
float3 cur = xform.Position;
float3 vel = float3.zero;
var cache = new EnemyAnimCache { Pos = cur, Hp = health.Current };
if (prevPos.TryGetValue(e, out var prev))
vel = (cur - prev) / dt;
prevPos[e] = cur;
{
vel = (cur - prev.Pos) / dt;
cache.ReactUntil = prev.ReactUntil;
cache.Heavy = prev.Heavy;
// 07-21 hit-react (G5): a replicated Health DROP on a living Husk = the flinch edge. The heavy
// tier (>= staggerDamage, e.g. the finisher) plays the stagger -- aligned with the server's B2
// poise break, so the anim never claims an interrupt the sim didn't make.
float drop = prev.Hp - health.Current;
if (drop > 0.01f && health.Current > 0f && reactSeconds > 0f)
{
cache.Heavy = drop >= staggerDamage;
cache.ReactUntil = el + (cache.Heavy ? staggerSeconds : reactSeconds);
}
}
prevPos[e] = cache;
float2 facing = AnimParamMath.PlanarForward(xform.Rotation);
float3 p = AnimParamMath.LocomotionParams(vel, facing, stats.MoveSpeed);
@@ -130,6 +162,10 @@ namespace ProjectM.Client
if (a.HasParameter(speed)) a.SetParameterValue(speed, p.z);
if (a.HasParameter(isAttacking)) a.SetParameterValue(isAttacking, attacking);
if (a.HasParameter(isDead)) a.SetParameterValue(isDead, dead);
// 07-21 hit-react pulse (a corpse never flinches; the controller adds the windup-honesty gate).
bool hitActive = !dead && cache.ReactUntil > 0f && el < cache.ReactUntil;
if (a.HasParameter(isHit)) a.SetParameterValue(isHit, hitActive);
if (a.HasParameter(isHitHeavy)) a.SetParameterValue(isHitHeavy, hitActive && cache.Heavy);
}
}
}
@@ -163,6 +163,15 @@ namespace ProjectM.Client
/// <summary>Camera hold frames when the melee FINISHER lands (07-20 G5 impact ladder; light hits use HitStopMaxFrames).</summary>
public static int FinisherHoldFrames;
// ---- 07-21 enemy hit-reacts (G5; the Rukhanka-safe flinch that replaced the cut vibrate) ----
/// <summary>Seconds the light flinch pulse holds (0 = hit-reacts OFF).</summary>
public static float HitReactSeconds;
/// <summary>Seconds the heavy stagger pulse holds.</summary>
public static float HitStaggerSeconds;
/// <summary>Health drop at/above which the react upgrades to the STAGGER tier (keep aligned with the
/// server's B2 poise math: the finisher should stagger, a light hit should not).</summary>
public static float HitReactStaggerDamage;
/// <summary>Master gate for gamepad rumble (no-op on KBM).</summary>
public static bool RumbleEnabled;
@@ -290,6 +299,9 @@ namespace ProjectM.Client
MeleeArcIntensity = 0.8f; // 07-21 HEAVY LOCK: the weapon is the read, the arc recedes further
MeleeArcBubbles = 6;
FinisherHoldFrames = 7; // 07-21 HEAVY LOCK: a fatter finisher beat (~117ms, still under the 8-frame ladder cap)
HitReactSeconds = 0.3f; // 07-21: light flinch pulse (clip plays at 1.4x -> the peak lands inside it)
HitStaggerSeconds = 0.55f; // heavy stagger pulse
HitReactStaggerDamage = 50f; // finisher (63 seeded) staggers, light (42) flinches -- tracks B2 poise
RumbleEnabled = true;
@@ -26,6 +26,8 @@ namespace ProjectM.EditorTools
const string PlayerController = "Assets/_Project/Animation/AC_PlayerTopDown.controller";
const string EnemyController = "Assets/_Project/Animation/AC_EnemyTopDown.controller";
const string AttackClip = "Assets/_Project/Animation/EnemyAttackWindup.anim";
const string HitReactFbx = "Assets/Synty/AnimationSwordCombat/Animations/Polygon/Hit/HitReact/A_Hit_F_React_Sword.fbx";
const string HitStaggerFbx = "Assets/Synty/AnimationSwordCombat/Animations/Polygon/Hit/HitStagger/A_Hit_F_Stagger_Sword.fbx";
const string WerewolfAtlas = "Assets/Synty/PolygonWerewolf/Textures/PolygonWerewolf_01_A.png";
const string KaijuAtlas = "Assets/Synty/PolygonKaiju/Textures/PolygonKaiju_01.png";
@@ -364,5 +366,83 @@ namespace ProjectM.EditorTools
}
Debug.LogWarning($"[EnemyRigTools] Field/prop '{name}' not found on {o.GetType().Name}.");
}
}
/// <summary>07-21 hit-react pass (guidelines G5; the Rukhanka-safe flinch replacing the cut vibrate):
/// adds IsHit/IsHitHeavy params + HitReact (light flinch) and HitStagger (heavy) Any-State states to
/// AC_EnemyTopDown, pulsed by EnemyAnimationDriveSystem off replicated Health-drop edges. HONESTY GATE:
/// the light react requires IsAttacking == false so a sub-poise hit never visually cancels a windup the
/// server is still running (the telegraph must not lie); a HEAVY hit staggers unconditionally, which
/// aligns with the server's B2 poise break (finisher knockback >= the stagger threshold zeroes the
/// windup). Idempotent / re-runnable.</summary>
[MenuItem("ProjectM/Animation/Enemy - Wire Hit Reacts (LANTERN)")]
public static void WireEnemyHitReacts()
{
var ac = AssetDatabase.LoadAssetAtPath<AnimatorController>(EnemyController);
if (ac == null) { Debug.LogError($"[EnemyRigTools] Controller missing: {EnemyController}"); return; }
AnimationClip Clip(string fbxPath, string clipName)
{
foreach (var o in AssetDatabase.LoadAllAssetsAtPath(fbxPath))
if (o is AnimationClip c && c.name == clipName) return c;
Debug.LogError($"[EnemyRigTools] Clip missing: {fbxPath} -> {clipName}");
return null;
}
var react = Clip(HitReactFbx, "A_Hit_F_React_Sword");
var stagger = Clip(HitStaggerFbx, "A_Hit_F_Stagger_Sword");
if (react == null || stagger == null) return;
void EnsureParam(string n)
{
foreach (var p in ac.parameters) if (p.name == n) return;
ac.AddParameter(n, AnimatorControllerParameterType.Bool);
}
EnsureParam("IsHit");
EnsureParam("IsHitHeavy");
var sm = ac.layers[0].stateMachine;
AnimatorState Find(string n)
{
foreach (var cs in sm.states) if (cs.state.name == n) return cs.state;
return null;
}
var hitReact = Find("HitReact");
if (hitReact == null)
{
hitReact = sm.AddState("HitReact");
var to = sm.AddAnyStateTransition(hitReact);
to.hasExitTime = false; to.duration = 0.05f; to.canTransitionToSelf = false;
to.AddCondition(AnimatorConditionMode.If, 0f, "IsHit");
to.AddCondition(AnimatorConditionMode.IfNot, 0f, "IsHitHeavy");
to.AddCondition(AnimatorConditionMode.IfNot, 0f, "IsAttacking"); // honesty: never cancel a live windup visual
to.AddCondition(AnimatorConditionMode.IfNot, 0f, "IsDead");
var ex = hitReact.AddExitTransition();
ex.hasExitTime = false; ex.duration = 0.10f;
ex.AddCondition(AnimatorConditionMode.IfNot, 0f, "IsHit");
}
hitReact.motion = react;
hitReact.speed = 1.4f; // 0.83s clip -> the flinch peak inside the ~0.3s pulse
var hitStagger = Find("HitStagger");
if (hitStagger == null)
{
hitStagger = sm.AddState("HitStagger");
var to = sm.AddAnyStateTransition(hitStagger);
to.hasExitTime = false; to.duration = 0.05f; to.canTransitionToSelf = false;
to.AddCondition(AnimatorConditionMode.If, 0f, "IsHit");
to.AddCondition(AnimatorConditionMode.If, 0f, "IsHitHeavy"); // heavy overrides even a windup (server poise broke it)
to.AddCondition(AnimatorConditionMode.IfNot, 0f, "IsDead");
var ex = hitStagger.AddExitTransition();
ex.hasExitTime = false; ex.duration = 0.12f;
ex.AddCondition(AnimatorConditionMode.IfNot, 0f, "IsHit");
}
hitStagger.motion = stagger;
hitStagger.speed = 1.2f; // 1.07s clip -> the lurch inside the ~0.55s pulse
EditorUtility.SetDirty(ac);
AssetDatabase.SaveAssets();
Debug.Log("[EnemyRigTools] Hit reacts wired: HitReact (light, windup-honest) + HitStagger (heavy) on AC_EnemyTopDown.");
}
}
}