fix(transform): return from monster forms without replaying the spawn animation

Hiding the human body with SetActive(false) on the player's "Visual" also
disabled the Animator that lives on it, and Unity rebinds an Animator when it
is re-enabled: the controller restarts from its entry state, which for the
player is the get-up clip played when joining a world. Every Skadi dragon form,
Bat Form and Mist Form therefore ended with the player standing back up off the
ground.

PlayerVisualHider hides the body by suppressing its renderers instead, leaving
the GameObject active so the Animator and ZSyncAnimation keep running for the
whole transformation. Hiding uses forceRenderingOff on top of Renderer.enabled,
which nothing else in the game writes on a character, so the body cannot be
un-hidden behind our back.

Applied to the local and remote paths of both races, and to the ZDO sync's
revert so a remote player who dies mid-form cannot stay invisible.
This commit is contained in:
kanym 2026-08-02 18:56:51 +02:00
commit 4793811978
6 changed files with 175 additions and 46 deletions

View file

@ -607,9 +607,9 @@ namespace RacesOfValheim
// Toggle visibility
if (isBat || isMist) {
if (playerVisual != null) playerVisual.gameObject.SetActive(false);
PlayerVisualHider.Hide(playerVisual, "aswid-remote-form");
} else {
if (playerVisual != null) playerVisual.gameObject.SetActive(true);
PlayerVisualHider.Show(playerVisual, "aswid-remote-revert");
}
}
}

View file

@ -545,10 +545,7 @@ namespace RacesOfValheim
}
Transform playerVisual = Player.m_localPlayer.transform.Find("Visual");
if (playerVisual != null)
{
playerVisual.gameObject.SetActive(false);
}
PlayerVisualHider.Hide(playerVisual, "aswid-local-bat");
GameObject batPrefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>("Bat");
if (batPrefab != null)
@ -583,15 +580,7 @@ namespace RacesOfValheim
}
Transform playerVisual = Player.m_localPlayer.transform.Find("Visual");
if (playerVisual != null)
{
playerVisual.gameObject.SetActive(true);
ZSyncAnimation zanim = Player.m_localPlayer.GetComponent<ZSyncAnimation>();
if (zanim != null) zanim.SetBool("sit", false);
Animator anim = playerVisual.GetComponent<Animator>();
if (anim != null) anim.SetBool("sit", false);
}
PlayerVisualHider.Show(playerVisual, "aswid-local-bat-revert");
Player.m_localPlayer.GetSEMan().RemoveStatusEffect("SE_BatForm".GetStableHashCode(), true);
if (Player.m_localPlayer.GetComponent<ZNetView>() != null) {
@ -680,11 +669,7 @@ namespace RacesOfValheim
yield return null;
}
if (playerVisual != null)
{
playerVisual.localScale = Vector3.one * 0.01f;
playerVisual.gameObject.SetActive(false);
}
PlayerVisualHider.Hide(playerVisual, "aswid-local-mist");
if (mistTransformation != null) mistTransformation.transform.localScale = Vector3.one;
isTransformingSequence = false;
@ -699,15 +684,8 @@ namespace RacesOfValheim
if (poofPrefab != null) Instantiate(poofPrefab, Player.m_localPlayer.transform.position, Quaternion.identity);
Transform playerVisual = Player.m_localPlayer.transform.Find("Visual");
if (playerVisual != null)
{
playerVisual.gameObject.SetActive(true);
ZSyncAnimation zanim = Player.m_localPlayer.GetComponent<ZSyncAnimation>();
if (zanim != null) zanim.SetBool("sit", false);
Animator anim = playerVisual.GetComponent<Animator>();
if (anim != null) anim.SetBool("sit", false);
}
// Renderers come back on here; the loop below still grows the body out of the mist.
PlayerVisualHider.Show(playerVisual, "aswid-local-mist-revert");
float elapsed = 0f;
float duration = 1.0f;

View file

@ -0,0 +1,162 @@
using System.Collections.Generic;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Hides and restores a player's "Visual" child without ever deactivating it.
///
/// The player's Animator sits on that same GameObject, and Unity rebinds an Animator whenever it
/// is re-enabled: the controller restarts from its entry state, which for the player is the same
/// get-up clip that plays when you spawn into a world. Deactivating the Visual to hide the human
/// body during a monster form is therefore why reverting looked exactly like respawning.
///
/// Hiding is done with Renderer.forceRenderingOff on top of Renderer.enabled. Nothing in the game
/// touches forceRenderingOff on a character (only EnvZone uses it, on its own props), so the body
/// stays invisible even if some other code re-enables the renderers behind our back.
/// </summary>
public class PlayerVisualHider : MonoBehaviour
{
// TEMPORARY: tracing for the transform-visibility work. Flip to false once multiplayer
// transforms are confirmed good, along with the rest of the debug logging in 1.3.9.
public const bool Diagnostics = true;
// VisEquipment rebuilds item visuals whenever gear changes and they come back visible, so
// the hidden set has to be re-swept rather than applied once.
private const float RescanInterval = 0.25f;
private const float LogInterval = 2f;
private struct HiddenRenderer
{
public Renderer Renderer;
public bool WasEnabled;
}
private readonly List<HiddenRenderer> m_hidden = new List<HiddenRenderer>();
private float m_nextRescan;
private float m_nextLog;
private string m_context = "";
public static void Hide(Transform playerVisual, string context)
{
if (playerVisual == null) return;
// Undo the two older ways this body could have been hidden, so a form entered right
// after one of those code paths still starts from a sane state.
if (!playerVisual.gameObject.activeSelf) playerVisual.gameObject.SetActive(true);
if (playerVisual.localScale != Vector3.one) playerVisual.localScale = Vector3.one;
// Belt and braces: if anything else ever does deactivate the Visual, at least keep the
// animator from snapping back to its spawn state.
Animator anim = playerVisual.GetComponent<Animator>();
if (anim != null) anim.keepAnimatorStateOnDisable = true;
PlayerVisualHider hider = playerVisual.GetComponent<PlayerVisualHider>();
if (hider == null) hider = playerVisual.gameObject.AddComponent<PlayerVisualHider>();
bool alreadyHiding = hider.enabled;
hider.m_context = context;
hider.enabled = true;
int added = hider.HideRenderers();
if (Diagnostics && (!alreadyHiding || added > 0))
{
Jotunn.Logger.LogInfo(
$"[VisualHider] hide '{context}' newlyHidden={added} tracked={hider.m_hidden.Count} " +
$"visualActive={playerVisual.gameObject.activeInHierarchy}");
}
}
public static void Show(Transform playerVisual, string context)
{
if (playerVisual == null) return;
if (!playerVisual.gameObject.activeSelf) playerVisual.gameObject.SetActive(true);
if (playerVisual.localScale != Vector3.one) playerVisual.localScale = Vector3.one;
PlayerVisualHider hider = playerVisual.GetComponent<PlayerVisualHider>();
if (hider == null || !hider.enabled) return;
int restored = hider.RestoreRenderers();
if (Diagnostics)
{
Jotunn.Logger.LogInfo(
$"[VisualHider] show '{context}' restored={restored} (hidden by '{hider.m_context}')");
}
}
public static bool IsHidden(Transform playerVisual)
{
if (playerVisual == null) return false;
PlayerVisualHider hider = playerVisual.GetComponent<PlayerVisualHider>();
return hider != null && hider.enabled;
}
/// <summary>Hides every renderer not already hidden by us. Returns how many were added.</summary>
private int HideRenderers()
{
int added = 0;
// Equipment is parented to the body's bones, so this covers weapons, armour, hair and
// the cape along with the body mesh itself.
foreach (Renderer r in GetComponentsInChildren<Renderer>(true))
{
if (r == null || r.forceRenderingOff) continue; // already ours
m_hidden.Add(new HiddenRenderer { Renderer = r, WasEnabled = r.enabled });
r.enabled = false;
r.forceRenderingOff = true;
added++;
}
m_nextRescan = Time.time + RescanInterval;
return added;
}
private int RestoreRenderers()
{
int restored = 0;
foreach (HiddenRenderer h in m_hidden)
{
if (h.Renderer == null) continue;
h.Renderer.forceRenderingOff = false;
h.Renderer.enabled = h.WasEnabled;
restored++;
}
m_hidden.Clear();
enabled = false;
return restored;
}
private void LateUpdate()
{
if (Time.time < m_nextRescan) return;
int added = HideRenderers();
if (!Diagnostics || Time.time < m_nextLog) return;
// If anything re-enables a renderer we own, forceRenderingOff still keeps it invisible -
// but we want to know who, so report it rather than silently papering over it.
int reEnabled = 0;
string firstName = "";
foreach (HiddenRenderer h in m_hidden)
{
if (h.Renderer == null || !h.Renderer.enabled) continue;
if (reEnabled == 0) firstName = h.Renderer.name;
reEnabled++;
}
if (added == 0 && reEnabled == 0) return;
m_nextLog = Time.time + LogInterval;
Jotunn.Logger.LogInfo(
$"[VisualHider] sweep '{m_context}' newlyHidden={added} reEnabledBehindUs={reEnabled} " +
$"first='{firstName}' tracked={m_hidden.Count}");
}
}
}

View file

@ -62,7 +62,7 @@ namespace RacesOfValheim
if (string.IsNullOrEmpty(formName))
{
if (playerVisual != null) playerVisual.gameObject.SetActive(true);
PlayerVisualHider.Show(playerVisual, "skadi-remote-revert");
return;
}
@ -84,7 +84,7 @@ namespace RacesOfValheim
}
}
if (playerVisual != null) playerVisual.gameObject.SetActive(false);
PlayerVisualHider.Hide(playerVisual, "skadi-remote-form");
}
}

View file

@ -637,7 +637,7 @@ namespace RacesOfValheim
if (prefab != null && playerVisual != null)
{
playerVisual.gameObject.SetActive(false);
PlayerVisualHider.Hide(playerVisual, "skadi-local-form");
Transform visual = prefab.transform.Find("Visual");
if (visual != null)
@ -722,16 +722,7 @@ namespace RacesOfValheim
var forms1 = Player.m_localPlayer.GetComponentsInChildren<DrakeFormBehavior>(true);
foreach (var f in forms1) Destroy(f.gameObject);
if (playerVisual != null)
{
playerVisual.localScale = Vector3.one;
playerVisual.gameObject.SetActive(true);
ZSyncAnimation zanim = Player.m_localPlayer.GetComponent<ZSyncAnimation>();
if (zanim != null) zanim.SetBool("sit", false);
Animator anim = playerVisual.GetComponent<Animator>();
if (anim != null) anim.SetBool("sit", false);
}
PlayerVisualHider.Show(playerVisual, "skadi-local-revert");
isTransformed = false;
formExpireTime = 0f;

View file

@ -241,10 +241,8 @@ namespace RacesOfValheim
if (m_player.transform.localScale != Vector3.one) m_player.transform.localScale = Vector3.one;
Transform playerVisual = m_player.transform.Find("Visual");
if (playerVisual != null && playerVisual.localScale != Vector3.one)
{
playerVisual.localScale = Vector3.one;
}
// Also clears a hidden body left behind if a remote player died mid-transformation.
PlayerVisualHider.Show(playerVisual, "zdosync-revert");
if (m_currentFormInstance != null) Destroy(m_currentFormInstance);
EnsureFireAuraVFX(false);