diff --git a/JotunnModStub/Dvergar/DvergarVisuals.cs b/JotunnModStub/Dvergar/DvergarVisuals.cs index 66a6928..bc83191 100644 --- a/JotunnModStub/Dvergar/DvergarVisuals.cs +++ b/JotunnModStub/Dvergar/DvergarVisuals.cs @@ -130,7 +130,11 @@ namespace RacesOfValheim { if (player == null) return; - Transform eyes = player.transform.Find(EyesName); + Animator animator = player.GetComponentInChildren(); + Transform headBone = animator != null ? animator.GetBoneTransform(HumanBodyBones.Head) : null; + if (headBone == null) return; // rig not up yet; try again next frame + + Transform eyes = headBone.Find(EyesName); if (!IsDvergarPlayer(player) || player.IsDead()) { @@ -144,24 +148,23 @@ namespace RacesOfValheim return; } - Animator animator = player.GetComponentInChildren(); - Transform headBone = animator != null ? animator.GetBoneTransform(HumanBodyBones.Head) : null; - if (headBone == null) return; // rig not up yet; try again next frame - - if (eyes == null) eyes = BuildEyes(player); + if (eyes == null) eyes = BuildEyes(player, headBone); if (eyes == null) return; // already logged once by the resolver - // Everything is expressed in the player's own frame and multiplied by the race's - // visual scale, so the eyes sit in a Dvergar's face at Dvergar size rather than where - // a full-height human's would have been. - float raceScale = GetVisualScale(player); - Transform root = player.transform; - - eyes.position = headBone.position - + root.forward * (DvergarConfig.EyeForward * raceScale) - + root.up * (DvergarConfig.EyeHeight * raceScale); - eyes.rotation = Quaternion.LookRotation(root.forward, root.up); - eyes.localScale = Vector3.one * raceScale; + // The holder is a child of the head bone, so position and rotation are inherited and + // the eyes are socketed rather than followed - the previous version drove them from + // the player root's axes each frame, which meant an idle animation could turn the head + // while the eyes stayed pointing wherever the body did. + // + // Only the scale still needs saying, because it can change under us: the bone carries + // the armature's own hundredfold scale multiplied by whatever the race is rendering + // at, and dividing it out leaves the holder standing at exactly the race's scale, so + // the offsets underneath stay honest metres. + float lossy = headBone.lossyScale.x; + if (Mathf.Abs(lossy) > 0.0001f) + { + eyes.localScale = Vector3.one * (GetVisualScale(player) / lossy); + } Color glow = GetEyeColor(player); foreach (Renderer renderer in eyes.GetComponentsInChildren(true)) @@ -193,17 +196,28 @@ namespace RacesOfValheim return DvergarConfig.BaseEyeColor; } - private static Transform BuildEyes(Player player) + private static Transform BuildEyes(Player player, Transform headBone) { Shader glow = ResolveGlowShader(); if (glow == null) return null; - // Under the player root rather than the rig: ApplyEyes drives the transform in world - // space every frame, and an unscaled parent keeps the offsets honest metres. GameObject holder = new GameObject(EyesName); - holder.transform.SetParent(player.transform, false); + holder.transform.SetParent(headBone, false); holder.transform.localPosition = Vector3.zero; - holder.transform.localRotation = Quaternion.identity; + + // Which way a rig bone points is nobody's business but the artist's, and the head + // bone's axes are not the model's. Rather than guess, ask: the body's own forward and + // up, expressed in the head bone's local space, are exactly the directions wanted - + // and because they are local they keep meaning the same thing as the head turns. + // Measured once, here, while the head is still looking where the body is. + Transform root = player.transform; + Vector3 localForward = headBone.InverseTransformDirection(root.forward).normalized; + Vector3 localUp = headBone.InverseTransformDirection(root.up).normalized; + + holder.transform.localRotation = localForward.sqrMagnitude > 0.0001f + ? Quaternion.LookRotation(localForward, localUp) + : Quaternion.identity; + holder.transform.localScale = Vector3.one; for (int i = 0; i < 2; i++)