fix(fenrir): hidden human no longer attacks or blocks under a form

The attack block patched Character.StartAttack, which Humanoid overrides
without calling the base, so it never ran; and it covered the Fenring only.
Every form swing also fired the hidden human's attack: stamina spent,
regen held at zero while it played, and forward_speed dropped to 0. Patch
Humanoid.StartAttack for all four forms, and clear m_blocking after
SetControls so the pad's heavy attack stops raising the guard.

Forms treat the player as airborne only after 0.25 s off the ground, so
bumps no longer flicker run to idle. Melee reach ignores the target's own
colliders and accepts a clear line from the eyes or the attack point, with
an opt-in hit log. Sunrise names the key that still works by day.
This commit is contained in:
kanym 2026-09-28 22:19:37 +02:00
commit b4b6fe3758
8 changed files with 323 additions and 153 deletions

View file

@ -393,6 +393,7 @@ namespace RacesOfValheim
private ZNetView nview;
private string lastAttack;
private GameObject flames;
private readonly FormLocomotion locomotion = new FormLocomotion();
// The flames hang off the player, not this model, so they outlive it unless taken down.
private void OnDestroy()
@ -418,20 +419,10 @@ namespace RacesOfValheim
ReplayAttack();
ReplayFire();
bool grounded = playerAnim.GetBool("onGround");
// All four borrowed creatures answer to these, and they are still written through
// try/catch the way the local behaviours do it: a controller missing one of them would
// otherwise throw once per frame, for every onlooker.
try { formAnim.SetBool("onGround", grounded); } catch {}
try { formAnim.SetBool("inWater", playerAnim.GetBool("inWater")); } catch {}
// The same three cases the local behaviours use, so both views of the same wolf agree:
// airborne and crouching each read as standing still. Player.IsCrouching answers off
// the animator's current state, which is replicated, so it is true here too.
bool still = !grounded || player.IsCrouching();
try { formAnim.SetFloat("forward_speed", still ? 0f : playerAnim.GetFloat("forward_speed")); } catch {}
try { formAnim.SetFloat("sideway_speed", still ? 0f : playerAnim.GetFloat("sideway_speed")); } catch {}
// The same helper the local behaviours use, so both views of the same wolf agree.
// Player.IsCrouching answers off the animator's current state, which is replicated, so
// it is true here too.
locomotion.Mirror(formAnim, playerAnim, player.IsCrouching());
}
// Written every frame while the flag is set, and only when the controller has the bool: a
@ -516,8 +507,11 @@ namespace RacesOfValheim
public class FenrirFormBehavior : MonoBehaviour
{
private const string FormLabel = "Fenring";
private Animator playerAnim;
private Animator fenrirAnim;
private readonly FormLocomotion locomotion = new FormLocomotion();
private bool isAttacking = false;
@ -533,34 +527,13 @@ namespace RacesOfValheim
{
if (playerAnim == null || fenrirAnim == null) return;
// Se stiamo attaccando, l'Update smette di inviare costantemente
// "onGround", inWater, forward_speed ecc. alle variabili di base?
// Se stiamo attaccando, l'Update smette di inviare costantemente
// "onGround", inWater, forward_speed ecc. alle variabili di base?
// Meglio continuare a sincronizzare i movimenti base.
if (!isAttacking)
{
bool isGrounded = playerAnim.GetBool("onGround");
bool isGrounded = locomotion.Mirror(fenrirAnim, playerAnim, Player.m_localPlayer.IsCrouching());
fenrirAnim.SetBool("onGround", isGrounded);
fenrirAnim.SetBool("inWater", playerAnim.GetBool("inWater"));
if (!isGrounded)
{
fenrirAnim.SetFloat("forward_speed", 0f);
fenrirAnim.SetFloat("sideway_speed", 0f);
}
else
{
if (Player.m_localPlayer.IsCrouching())
{
fenrirAnim.SetFloat("forward_speed", 0f);
fenrirAnim.SetFloat("sideway_speed", 0f);
}
else
{
fenrirAnim.SetFloat("forward_speed", playerAnim.GetFloat("forward_speed"));
fenrirAnim.SetFloat("sideway_speed", playerAnim.GetFloat("sideway_speed"));
}
}
if (isGrounded && RaceInput.Allowed)
{
if (FenrirConfig.HowlKey.Value.IsDownIgnoringHeldKeys())
@ -717,6 +690,7 @@ namespace RacesOfValheim
int layerMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "character", "character_net", "character_ghost", "hitbox", "character_noenv", "vehicle");
Collider[] hits = Physics.OverlapSphere(attackPoint, attackRadius, layerMask);
FormMelee.LogSwing(FormLabel, hits.Length);
System.Collections.Generic.HashSet<GameObject> hitObjects = new System.Collections.Generic.HashSet<GameObject>();
foreach (Collider hit in hits)
@ -731,21 +705,25 @@ namespace RacesOfValheim
Character characterTarget = target as Character;
if (characterTarget != null && characterTarget == Player.m_localPlayer) continue;
// Marked as hit only once it IS hit, further down. Marking it here let the first
// collider found decide for the whole creature: a limb outside the arc or behind
// cover ruled out a body that was squarely in the swing.
GameObject targetObj = (target as MonoBehaviour)?.gameObject;
if (targetObj != null)
{
if (hitObjects.Contains(targetObj)) continue;
hitObjects.Add(targetObj);
}
if (targetObj != null && hitObjects.Contains(targetObj)) continue;
Vector3 targetCenter = hit.bounds.center;
Vector3 dirToTarget = (targetCenter - playerPos).normalized;
float angle = Vector3.Angle(Player.m_localPlayer.transform.forward, dirToTarget);
if (angle < 70f)
if (angle >= 70f) FormMelee.LogMiss(FormLabel, hit, $"outside the swing arc ({angle:0} deg)");
else
{
Vector3 eyePos = playerPos + Vector3.up * 1.5f;
if (!Physics.Linecast(eyePos, targetCenter, out RaycastHit rayHit, LayerMask.GetMask("Default", "static_solid", "terrain", "piece")))
if (!FormMelee.CanReach(eyePos, attackPoint, hit, target, out string cover))
{
FormMelee.LogMiss(FormLabel, hit, "no clear line, blocked by '" + cover + "'");
}
else
{
HitData hitData = new HitData();
hitData.m_damage.m_slash = totalDamage;
@ -759,6 +737,8 @@ namespace RacesOfValheim
hitData.m_toolTier = 0;
hitData.SetAttacker(Player.m_localPlayer);
if (targetObj != null) hitObjects.Add(targetObj);
FormMelee.LogHit(FormLabel, target, totalDamage);
target.Damage(hitData);
}
}
@ -770,8 +750,11 @@ namespace RacesOfValheim
// --- CLASSE CUSTOM PER L'ATTACCO "FIRE" DEL CULTIST ---
public class CultistFormBehavior : MonoBehaviour
{
private const string FormLabel = "Cultist";
private Animator playerAnim;
private Animator cultistAnim;
private readonly FormLocomotion locomotion = new FormLocomotion();
private bool isAttacking = false;
private bool isShootingFire = false;
@ -790,30 +773,8 @@ namespace RacesOfValheim
if (!isAttacking)
{
bool isGrounded = playerAnim.GetBool("onGround");
cultistAnim.SetBool("onGround", isGrounded);
cultistAnim.SetBool("inWater", playerAnim.GetBool("inWater"));
if (!isGrounded)
{
cultistAnim.SetFloat("forward_speed", 0f);
cultistAnim.SetFloat("sideway_speed", 0f);
}
else
{
// Se tiene premuto Ctrl (Crouch), rallenta o ferma il movimento per preparare l'attacco
if (Player.m_localPlayer.IsCrouching())
{
cultistAnim.SetFloat("forward_speed", 0f);
cultistAnim.SetFloat("sideway_speed", 0f);
}
else
{
cultistAnim.SetFloat("forward_speed", playerAnim.GetFloat("forward_speed"));
cultistAnim.SetFloat("sideway_speed", playerAnim.GetFloat("sideway_speed"));
}
}
// Se tiene premuto Ctrl (Crouch), ferma il movimento per preparare l'attacco
bool isGrounded = locomotion.Mirror(cultistAnim, playerAnim, Player.m_localPlayer.IsCrouching());
if (isGrounded)
{
@ -988,6 +949,7 @@ namespace RacesOfValheim
int layerMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "character", "character_net", "character_ghost", "hitbox", "character_noenv", "vehicle");
Collider[] hits = Physics.OverlapSphere(attackPoint, attackRadius, layerMask);
FormMelee.LogSwing(FormLabel, hits.Length);
System.Collections.Generic.HashSet<GameObject> hitObjects = new System.Collections.Generic.HashSet<GameObject>();
foreach (Collider hit in hits)
@ -1002,12 +964,11 @@ namespace RacesOfValheim
Character characterTarget = target as Character;
if (characterTarget != null && characterTarget == Player.m_localPlayer) continue;
// Marked as hit only once it IS hit, further down. Marking it here let the first
// collider found decide for the whole creature: a limb outside the arc or behind
// cover ruled out a body that was squarely in the swing.
GameObject targetObj = (target as MonoBehaviour)?.gameObject;
if (targetObj != null)
{
if (hitObjects.Contains(targetObj)) continue;
hitObjects.Add(targetObj);
}
if (targetObj != null && hitObjects.Contains(targetObj)) continue;
Vector3 targetCenter = hit.bounds.center;
Vector3 dirToTarget = (targetCenter - playerPos).normalized;
@ -1025,6 +986,8 @@ namespace RacesOfValheim
hitData.m_hitType = HitData.HitType.PlayerHit;
hitData.SetAttacker(Player.m_localPlayer);
if (targetObj != null) hitObjects.Add(targetObj);
FormMelee.LogHit(FormLabel, target, totalDamage);
target.Damage(hitData);
if (ZNetScene.instance != null)
@ -1103,6 +1066,7 @@ namespace RacesOfValheim
int layerMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "character", "character_net", "character_ghost", "hitbox", "character_noenv", "vehicle");
Collider[] hits = Physics.OverlapSphere(attackPoint, attackRadius, layerMask);
FormMelee.LogSwing(FormLabel, hits.Length);
System.Collections.Generic.HashSet<GameObject> hitObjects = new System.Collections.Generic.HashSet<GameObject>();
foreach (Collider hit in hits)
@ -1117,21 +1081,25 @@ namespace RacesOfValheim
Character characterTarget = target as Character;
if (characterTarget != null && characterTarget == Player.m_localPlayer) continue;
// Marked as hit only once it IS hit, further down. Marking it here let the first
// collider found decide for the whole creature: a limb outside the arc or behind
// cover ruled out a body that was squarely in the swing.
GameObject targetObj = (target as MonoBehaviour)?.gameObject;
if (targetObj != null)
{
if (hitObjects.Contains(targetObj)) continue;
hitObjects.Add(targetObj);
}
if (targetObj != null && hitObjects.Contains(targetObj)) continue;
Vector3 targetCenter = hit.bounds.center;
Vector3 dirToTarget = (targetCenter - playerPos).normalized;
float angle = Vector3.Angle(Player.m_localPlayer.transform.forward, dirToTarget);
if (angle < 70f)
if (angle >= 70f) FormMelee.LogMiss(FormLabel, hit, $"outside the swing arc ({angle:0} deg)");
else
{
Vector3 eyePos = playerPos + Vector3.up * 1.5f;
if (!Physics.Linecast(eyePos, targetCenter, out RaycastHit rayHit, LayerMask.GetMask("Default", "static_solid", "terrain", "piece")))
if (!FormMelee.CanReach(eyePos, attackPoint, hit, target, out string cover))
{
FormMelee.LogMiss(FormLabel, hit, "no clear line, blocked by '" + cover + "'");
}
else
{
HitData hitData = new HitData();
hitData.m_damage.m_slash = totalDamage;
@ -1145,6 +1113,8 @@ namespace RacesOfValheim
hitData.m_toolTier = 0;
hitData.SetAttacker(Player.m_localPlayer);
if (targetObj != null) hitObjects.Add(targetObj);
FormMelee.LogHit(FormLabel, target, totalDamage);
target.Damage(hitData);
}
}
@ -1155,8 +1125,11 @@ namespace RacesOfValheim
public class WolfFormBehavior : MonoBehaviour
{
private const string FormLabel = "Wolf";
private Animator playerAnim;
private Animator wolfAnim;
private readonly FormLocomotion locomotion = new FormLocomotion();
private bool isAttacking = false;
void Start()
@ -1173,29 +1146,7 @@ namespace RacesOfValheim
if (!isAttacking)
{
bool isGrounded = playerAnim.GetBool("onGround");
try { wolfAnim.SetBool("onGround", isGrounded); } catch {}
try { wolfAnim.SetBool("inWater", playerAnim.GetBool("inWater")); } catch {}
if (!isGrounded)
{
try { wolfAnim.SetFloat("forward_speed", 0f); } catch {}
try { wolfAnim.SetFloat("sideway_speed", 0f); } catch {}
}
else
{
if (Player.m_localPlayer.IsCrouching())
{
try { wolfAnim.SetFloat("forward_speed", 0f); } catch {}
try { wolfAnim.SetFloat("sideway_speed", 0f); } catch {}
}
else
{
try { wolfAnim.SetFloat("forward_speed", playerAnim.GetFloat("forward_speed")); } catch {}
try { wolfAnim.SetFloat("sideway_speed", playerAnim.GetFloat("sideway_speed")); } catch {}
}
}
bool isGrounded = locomotion.Mirror(wolfAnim, playerAnim, Player.m_localPlayer.IsCrouching());
if (isGrounded && RaceInput.Allowed)
{
@ -1304,6 +1255,7 @@ namespace RacesOfValheim
int layerMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "character", "character_net", "character_ghost", "hitbox", "character_noenv", "vehicle");
Collider[] hits = Physics.OverlapSphere(attackPoint, attackRadius, layerMask);
FormMelee.LogSwing(FormLabel, hits.Length);
System.Collections.Generic.HashSet<GameObject> hitObjects = new System.Collections.Generic.HashSet<GameObject>();
foreach (Collider hit in hits)
@ -1318,21 +1270,25 @@ namespace RacesOfValheim
Character characterTarget = target as Character;
if (characterTarget != null && characterTarget == Player.m_localPlayer) continue;
// Marked as hit only once it IS hit, further down. Marking it here let the first
// collider found decide for the whole creature: a limb outside the arc or behind
// cover ruled out a body that was squarely in the swing.
GameObject targetObj = (target as MonoBehaviour)?.gameObject;
if (targetObj != null)
{
if (hitObjects.Contains(targetObj)) continue;
hitObjects.Add(targetObj);
}
if (targetObj != null && hitObjects.Contains(targetObj)) continue;
Vector3 targetCenter = hit.bounds.center;
Vector3 dirToTarget = (targetCenter - playerPos).normalized;
float angle = Vector3.Angle(Player.m_localPlayer.transform.forward, dirToTarget);
if (angle < 70f)
if (angle >= 70f) FormMelee.LogMiss(FormLabel, hit, $"outside the swing arc ({angle:0} deg)");
else
{
Vector3 eyePos = playerPos + Vector3.up * 1.0f;
if (!Physics.Linecast(eyePos, targetCenter, out RaycastHit rayHit, LayerMask.GetMask("Default", "static_solid", "terrain", "piece")))
if (!FormMelee.CanReach(eyePos, attackPoint, hit, target, out string cover))
{
FormMelee.LogMiss(FormLabel, hit, "no clear line, blocked by '" + cover + "'");
}
else
{
HitData hitData = new HitData();
hitData.m_damage.m_slash = totalDamage;
@ -1346,6 +1302,8 @@ namespace RacesOfValheim
hitData.m_toolTier = 0;
hitData.SetAttacker(Player.m_localPlayer);
if (targetObj != null) hitObjects.Add(targetObj);
FormMelee.LogHit(FormLabel, target, totalDamage);
target.Damage(hitData);
}
}
@ -1357,8 +1315,11 @@ namespace RacesOfValheim
// --- CLASSE CUSTOM PER L'ULV ---
public class UlvFormBehavior : MonoBehaviour
{
private const string FormLabel = "Ulv";
private Animator playerAnim;
private Animator ulvAnim;
private readonly FormLocomotion locomotion = new FormLocomotion();
private bool isAttacking = false;
@ -1375,29 +1336,7 @@ namespace RacesOfValheim
if (!isAttacking)
{
bool isGrounded = playerAnim.GetBool("onGround");
try { ulvAnim.SetBool("onGround", isGrounded); } catch {}
try { ulvAnim.SetBool("inWater", playerAnim.GetBool("inWater")); } catch {}
if (!isGrounded)
{
try { ulvAnim.SetFloat("forward_speed", 0f); } catch {}
try { ulvAnim.SetFloat("sideway_speed", 0f); } catch {}
}
else
{
if (Player.m_localPlayer.IsCrouching())
{
try { ulvAnim.SetFloat("forward_speed", 0f); } catch {}
try { ulvAnim.SetFloat("sideway_speed", 0f); } catch {}
}
else
{
try { ulvAnim.SetFloat("forward_speed", playerAnim.GetFloat("forward_speed")); } catch {}
try { ulvAnim.SetFloat("sideway_speed", playerAnim.GetFloat("sideway_speed")); } catch {}
}
}
bool isGrounded = locomotion.Mirror(ulvAnim, playerAnim, Player.m_localPlayer.IsCrouching());
if (isGrounded && RaceInput.Allowed)
{
@ -1512,6 +1451,7 @@ namespace RacesOfValheim
int layerMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "character", "character_net", "character_ghost", "hitbox", "character_noenv", "vehicle");
Collider[] hits = Physics.OverlapSphere(attackPoint, attackRadius, layerMask);
FormMelee.LogSwing(FormLabel, hits.Length);
System.Collections.Generic.HashSet<GameObject> hitObjects = new System.Collections.Generic.HashSet<GameObject>();
foreach (Collider hit in hits)
@ -1526,21 +1466,25 @@ namespace RacesOfValheim
Character characterTarget = target as Character;
if (characterTarget != null && characterTarget == Player.m_localPlayer) continue;
// Marked as hit only once it IS hit, further down. Marking it here let the first
// collider found decide for the whole creature: a limb outside the arc or behind
// cover ruled out a body that was squarely in the swing.
GameObject targetObj = (target as MonoBehaviour)?.gameObject;
if (targetObj != null)
{
if (hitObjects.Contains(targetObj)) continue;
hitObjects.Add(targetObj);
}
if (targetObj != null && hitObjects.Contains(targetObj)) continue;
Vector3 targetCenter = hit.bounds.center;
Vector3 dirToTarget = (targetCenter - playerPos).normalized;
float angle = Vector3.Angle(Player.m_localPlayer.transform.forward, dirToTarget);
if (angle < 70f)
if (angle >= 70f) FormMelee.LogMiss(FormLabel, hit, $"outside the swing arc ({angle:0} deg)");
else
{
Vector3 eyePos = playerPos + Vector3.up * 1.0f;
if (!Physics.Linecast(eyePos, targetCenter, out RaycastHit rayHit, LayerMask.GetMask("Default", "static_solid", "terrain", "piece")))
if (!FormMelee.CanReach(eyePos, attackPoint, hit, target, out string cover))
{
FormMelee.LogMiss(FormLabel, hit, "no clear line, blocked by '" + cover + "'");
}
else
{
HitData hitData = new HitData();
hitData.m_damage.m_slash = totalDamage;
@ -1554,6 +1498,8 @@ namespace RacesOfValheim
hitData.m_toolTier = 0;
hitData.SetAttacker(Player.m_localPlayer);
if (targetObj != null) hitObjects.Add(targetObj);
FormMelee.LogHit(FormLabel, target, totalDamage);
target.Damage(hitData);
}
}