diff --git a/JotunnModStub/Fenrir/FenrirFormShared.cs b/JotunnModStub/Fenrir/FenrirFormShared.cs index a6eb382..3d33ddf 100644 --- a/JotunnModStub/Fenrir/FenrirFormShared.cs +++ b/JotunnModStub/Fenrir/FenrirFormShared.cs @@ -129,9 +129,75 @@ namespace RacesOfValheim } wasSitting = sitting; + PlaceOnChair(form, player); + return grounded; } + // How far in front of the seat a creature on a chair settles, in metres. + private const float ChairForward = 0.5f; + private static readonly int ChairGroundMask = LayerMask.GetMask("Default", "static_solid", "terrain", "piece", "vehicle"); + private static readonly RaycastHit[] chairHits = new RaycastHit[16]; + private Transform placedRoot; + + /// + /// On a chair the player sits on the seat itself, and a creature there stood inside the chair, + /// at seat height. It goes half a metre in front of the seat instead, standing on whatever is + /// under that point. Moves the form's own root (the child of the player), never the player. + /// The sit key is left alone: the player is on the ground already. + /// + private void PlaceOnChair(Animator form, Player player) + { + Transform root = RootUnder(form.transform, player != null ? player.transform : null); + if (root == null) return; + + bool onChair = player.IsSitting(); + if (!onChair) + { + if (placedRoot == root && root.localPosition != Vector3.zero) root.localPosition = Vector3.zero; + return; + } + placedRoot = root; + + Transform body = player.transform; + Piece seat = SeatUnder(body.position); + Vector3 target = body.position + body.forward * ChairForward; + + float groundY = target.y; + int count = Physics.RaycastNonAlloc(target + Vector3.up * 1.5f, Vector3.down, chairHits, 4f, ChairGroundMask); + float best = float.MaxValue; + for (int i = 0; i < count; i++) + { + Collider c = chairHits[i].collider; + if (c == null || c.transform.IsChildOf(body)) continue; + if (seat != null && c.GetComponentInParent() == seat) continue; + if (chairHits[i].distance < best) { best = chairHits[i].distance; groundY = chairHits[i].point.y; } + } + + root.localPosition = body.InverseTransformPoint(new Vector3(target.x, groundY, target.z)); + } + + /// The chair or bench the player sits on: the first piece straight under them. + private static Piece SeatUnder(Vector3 position) + { + int count = Physics.RaycastNonAlloc(position + Vector3.up * 0.3f, Vector3.down, chairHits, 1.5f, ChairGroundMask); + Piece seat = null; + float best = float.MaxValue; + for (int i = 0; i < count; i++) + { + Piece piece = chairHits[i].collider != null ? chairHits[i].collider.GetComponentInParent() : null; + if (piece != null && chairHits[i].distance < best) { best = chairHits[i].distance; seat = piece; } + } + return seat; + } + + private static Transform RootUnder(Transform t, Transform player) + { + if (player == null) return null; + while (t != null && t.parent != player) t = t.parent; + return t; + } + /// /// The sit key, or a chair. The sit key is an EMOTE, not the "sitting" animator tag that /// Humanoid.IsSitting asks about - that tag belongs to chairs and benches - so IsSitting alone