fix(fenrir): a form on a chair settles in front of the seat
On a chair the player sits on the seat, and the form stood inside the chair at seat height. The form's root now goes 0.5 m in front of the seat, on whatever is under that point, skipping the seat's own colliders. The seat is found by a short raycast under the player, so it works for onlookers too, who have no attach point. Back to zero on standing up; the sit key is untouched.
This commit is contained in:
parent
e83c3c5d74
commit
5c0f092c89
1 changed files with 66 additions and 0 deletions
|
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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<Piece>() == 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));
|
||||
}
|
||||
|
||||
/// <summary>The chair or bench the player sits on: the first piece straight under them.</summary>
|
||||
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<Piece>() : 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue