The form hides the player's body by shrinking its Visual to 0.001, and RaceEyeHeight reads anything that small as hidden and leaves the eye where it was, at a person's height. The Muspell now measures its head height when the clone is made and hands RaceEyeHeight that height times the form's current scale, so the camera follows the grow and shrink.
105 lines
4.8 KiB
C#
105 lines
4.8 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.
|
|
///
|
|
/// 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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
}
|
|
}
|
|
}
|