RacesOfValheim/JotunnModStub/RaceEyeHeight.cs
kanym d502eda748 fix(visuals): a taller player reaches as far as they are tall
Vanilla only takes a hover target within m_maxInteractDistance (5 m) of
the eye, and RaceEyeHeight raises the eye with the model. A True Form
Trol's eye stands about 4 m up, so a log at its feet was already out of
reach and Grab Log worked on one small spot under the camera. The range
now grows by the same ratio as the eye (10-12.5 m in True Form, the
Muspell by its own eye), and never drops below vanilla for smaller races.
2026-09-30 13:57:51 +02:00

139 lines
6.5 KiB
C#

using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Puts the camera at the height of the body the player is actually wearing.
///
/// Every race that resizes a player does it on the "Visual" child and never on the root: the
/// root carries the collider and the camera pivot, and scaling that drops the player through
/// the ground they are standing on. The cost of that - which is what testers reported - is
/// that Character.m_eye hangs off the same unscaled root. GameCamera.GetCameraBaseOffset takes
/// the camera from m_eye.position minus the root's, and aim raycasts start there too, so a
/// Trol grew a head taller for everyone else while still seeing the world from human eye
/// level, and a Dvergar shrank without ever having to look up at anything. The height was
/// real, and only the other players could see it.
///
/// The eye is moved by whatever factor the Visual is wearing at that instant, rather than by a
/// per-race number: that way it follows the Trol's transformation lerp for free, costs nothing
/// for a race that is not sizing anybody (the factor is 1), and returns to the measured
/// original the moment a player stops being of that race.
///
/// Races that scale the player root instead - the Skeleton is the one - are already carrying
/// their eye with them, and read as a factor of 1 here, so they are left alone by
/// construction. So is a player inside a borrowed form (wolf, drake, bat): those hide the
/// body's renderers and leave its scale at 1, and the form's own camera handling is its
/// business. The Trenta's Muspell is the exception: it hands over its own eye height, see
/// TrentaMuspell.TryGetEyeHeight.
///
/// The interaction range follows the eye up, never down: see ApplyReach.
///
/// Applied to every player, not just the one at the keyboard. m_eye is also what a mob aims
/// at, so a Dvergar's head being where the Dvergar's head is matters on every client.
/// </summary>
public static class RaceEyeHeight
{
// Below this the body is not small, it is being hidden - an old scale-to-zero trick, or a
// transformation caught mid-lerp. Dividing the player's viewpoint down with it would put
// the camera inside the ground, so the eye is left where it last was.
private const float MinScale = 0.05f;
/// <summary>
/// Remembers where the eye sat before any race touched it, on the eye itself, so it dies
/// with the player object and can never be read back as an already-scaled value.
/// </summary>
private class EyeRest : MonoBehaviour
{
public Vector3 LocalPosition;
/// <summary>Player.m_maxInteractDistance before any race touched it. Kept here for the
/// same reason the eye is: read back later it would already be scaled.</summary>
public float InteractDistance;
}
public static void Apply(Player player)
{
if (player == null) return;
Transform eye = Traverse.Create(player).Field("m_eye").GetValue<Transform>();
if (eye == null) return;
GameObject visual = Traverse.Create(player).Field("m_visual").GetValue<GameObject>();
if (visual == null) return;
// If this build ever parents the eye inside the rig, the body scale already carries
// it and doing the same thing here would apply it twice.
if (eye.IsChildOf(visual.transform)) return;
EyeRest rest = eye.GetComponent<EyeRest>();
if (rest == null)
{
rest = eye.gameObject.AddComponent<EyeRest>();
rest.LocalPosition = eye.localPosition;
rest.InteractDistance = player.m_maxInteractDistance;
}
Vector3 wanted;
if (TrentaMuspell.TryGetEyeHeight(player, out float formEye))
{
// The one borrowed form that sets its own eye: the Muspell is too tall to look out
// of from a person's height. Asked before the hidden-body check below, because the
// Muspell is exactly what hides the body.
wanted = new Vector3(rest.LocalPosition.x, formEye, rest.LocalPosition.z);
}
else
{
float scale = visual.transform.localScale.y;
if (scale < MinScale) return;
wanted = rest.LocalPosition * scale;
}
if ((eye.localPosition - wanted).sqrMagnitude > 0.000001f)
{
eye.localPosition = wanted;
}
ApplyReach(player, rest, wanted);
}
/// <summary>
/// Stretches the interaction range by as much as the eye was raised.
///
/// Vanilla takes a hover target only within m_maxInteractDistance (5 m) of the EYE, so
/// raising the eye cost reach: a True Form Trol's eye stands about 4 m up, the log at its
/// feet was already 4 m away, and there was one small spot under the camera where Grab Log
/// worked at all. The ratio is taken off the eye itself rather than the Visual, so the
/// Muspell - whose Visual is shrunk to nothing while its eye is raised - gets the reach of
/// the size it looks out from.
///
/// Never below vanilla: a Furling or a Dvergar is shorter, and losing reach for it would
/// make every chest and door fiddly for no gain.
/// </summary>
private static void ApplyReach(Player player, EyeRest rest, Vector3 wantedEye)
{
if (rest.InteractDistance <= 0f || rest.LocalPosition.y <= 0.0001f) return;
float ratio = Mathf.Max(1f, wantedEye.y / rest.LocalPosition.y);
float reach = rest.InteractDistance * ratio;
if (!Mathf.Approximately(player.m_maxInteractDistance, reach))
{
player.m_maxInteractDistance = reach;
}
}
/// <summary>
/// Driven from Player.Update, the same place the scales themselves are applied from. The
/// order the postfixes run in does not matter: this reads whatever scale is on the Visual
/// now, so the worst a late scale can cost is one frame.
/// </summary>
[HarmonyPatch(typeof(Player), "Update")]
public static class Player_Update_EyeHeight_Patch
{
public static void Postfix(Player __instance)
{
Apply(__instance);
}
}
}
}