- Held tools sit lower and further right; the hammer faces forward and strikes about the camera's right - Headlamp leaves held items, treats and vending machines alone (rendering layer NoLamp); other lights keep lighting them - Hay grounded on the floor in world space (it floated) - Deck fence opens at each ladder, rail colliders split around it; ladders moved to 2 m off the edge middle, clear of the intakes; climbing goes over a rail only if one is in the way - Crew stand to the soil's up (Planet.Level): flat round the base, the ball's up past it, so the horizon stays level on a small planet - Convex roof hulls for the crew cabin, garage, pitched roof and windmill tower; cabin box lowered to the eaves - Rain and acid rain hits differ by surface: soil, grass, wood, metal - Idle motion: dishes pan, sways sway, sensors turn, eyes blink, windmill turns in the breeze, reels and parked rotors tick over - Belt: take tools off at the supply wall; hold E at a vending machine for a pack of 3 on the belt, LMB has one - New meadow pieces: boulders, slab stones, crystals, mushrooms, stump, bulb plants - -demofix checks for all of the above; loc rows in all 11 tables Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
5.4 KiB
C#
121 lines
5.4 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace GrassCutter
|
|
{
|
|
/// <summary>
|
|
/// The crew's collision capsule. Unity's CharacterController is welded to the world's Y axis, which is no good on a
|
|
/// ball, so this one stands along the local up: it sweeps through solid things (deck, machines, rocks), climbs low
|
|
/// steps, and takes the soil straight from <see cref="Planet.Altitude"/>.
|
|
/// </summary>
|
|
[RequireComponent(typeof(CapsuleCollider))]
|
|
public class CrewBody : MonoBehaviour
|
|
{
|
|
const float Skin = 0.03f, StepUp = 0.4f, FloorDot = 0.6f, StickDown = 0.3f;
|
|
const int Solid = (1 << Layers.Default) | (1 << Physics.IgnoreRaycastLayer) | (1 << Layers.Prop) | (1 << Layers.Riding) | (1 << Layers.Vehicle) | (1 << Layers.Player) | (1 << Layers.Build);
|
|
|
|
public bool isGrounded { get; private set; }
|
|
/// <summary>Walked into something: what was hit and which way we were going.</summary>
|
|
public event Action<RaycastHit, Vector3> Bumped;
|
|
|
|
CapsuleCollider _cap;
|
|
readonly Collider[] _near = new Collider[16];
|
|
|
|
void Awake() { _cap = GetComponent<CapsuleCollider>(); }
|
|
void OnEnable() { if (_cap) _cap.enabled = true; }
|
|
void OnDisable() { if (_cap) _cap.enabled = false; isGrounded = false; }
|
|
|
|
void Ends(Vector3 pos, Vector3 up, out Vector3 low, out Vector3 high)
|
|
{
|
|
low = pos + up * _cap.radius;
|
|
high = pos + up * (_cap.height - _cap.radius);
|
|
}
|
|
|
|
public void Move(Vector3 delta)
|
|
{
|
|
var pos = transform.position;
|
|
// the body stands the way the crew member stands (the soil's up: see Planet.Level)
|
|
var up = transform.up;
|
|
float rise = Vector3.Dot(delta, up);
|
|
var flat = delta - up * rise;
|
|
bool was = isGrounded;
|
|
isGrounded = false;
|
|
|
|
Unstick(ref pos, up);
|
|
float lifted = was ? Sweep(ref pos, up, StepUp, up) : 0f;
|
|
Slide(ref pos, flat, up);
|
|
if (rise > 0f && !was) Sweep(ref pos, up, rise, up);
|
|
else if (rise > 0f) Sweep(ref pos, up, rise - Mathf.Min(rise, lifted), up);
|
|
if (rise <= 0f || lifted > rise)
|
|
{
|
|
float down = Mathf.Max(0f, lifted - Mathf.Max(0f, rise)) - Mathf.Min(0f, rise);
|
|
if (was && rise <= 0f) down += 0.04f;
|
|
if (Physics.CapsuleCast(Low(pos, up), High(pos, up), _cap.radius - Skin, -up, out var hit, down + Skin, Solid, QueryTriggerInteraction.Ignore))
|
|
{
|
|
if (hit.distance > 0f) pos -= up * Mathf.Max(0f, hit.distance - Skin);
|
|
if (Vector3.Dot(hit.normal, up) >= FloorDot) isGrounded = true;
|
|
}
|
|
else pos -= up * down;
|
|
}
|
|
|
|
// the soil itself
|
|
float alt = Planet.Altitude(pos);
|
|
if (alt <= 0f || (was && rise <= 0f && alt < StickDown && !isGrounded))
|
|
{
|
|
pos -= Planet.Up(pos) * alt;
|
|
isGrounded = true;
|
|
}
|
|
transform.position = pos;
|
|
}
|
|
|
|
Vector3 Low(Vector3 pos, Vector3 up) { Ends(pos, up, out var l, out _); return l; }
|
|
Vector3 High(Vector3 pos, Vector3 up) { Ends(pos, up, out _, out var h); return h; }
|
|
|
|
/// <summary>Move straight along dir until something stops us. Returns how far we got.</summary>
|
|
float Sweep(ref Vector3 pos, Vector3 dir, float dist, Vector3 up)
|
|
{
|
|
if (dist <= 0f) return 0f;
|
|
float go = dist;
|
|
if (Physics.CapsuleCast(Low(pos, up), High(pos, up), _cap.radius - Skin, dir, out var hit, dist + Skin, Solid, QueryTriggerInteraction.Ignore))
|
|
go = hit.distance > 0f ? Mathf.Max(0f, hit.distance - Skin) : 0f;
|
|
pos += dir * go;
|
|
return go;
|
|
}
|
|
|
|
void Slide(ref Vector3 pos, Vector3 move, Vector3 up)
|
|
{
|
|
for (int i = 0; i < 4 && move.sqrMagnitude > 1e-8f; i++)
|
|
{
|
|
float dist = move.magnitude;
|
|
var dir = move / dist;
|
|
if (!Physics.CapsuleCast(Low(pos, up), High(pos, up), _cap.radius - Skin, dir, out var hit, dist + Skin, Solid, QueryTriggerInteraction.Ignore))
|
|
{
|
|
pos += move;
|
|
return;
|
|
}
|
|
if (hit.distance <= 0f) { Unstick(ref pos, up); continue; }
|
|
float go = Mathf.Max(0f, hit.distance - Skin);
|
|
pos += dir * go;
|
|
Bumped?.Invoke(hit, dir);
|
|
move = Vector3.ProjectOnPlane(dir * (dist - go), hit.normal);
|
|
// a wall never lifts you: only a walkable slope may
|
|
if (Vector3.Dot(hit.normal, up) < FloorDot) move -= up * Mathf.Max(0f, Vector3.Dot(move, up));
|
|
}
|
|
}
|
|
|
|
/// <summary>Spawned, teleported or shoved into something: step out of it.</summary>
|
|
void Unstick(ref Vector3 pos, Vector3 up)
|
|
{
|
|
int n = Physics.OverlapCapsuleNonAlloc(Low(pos, up), High(pos, up), _cap.radius, _near, Solid, QueryTriggerInteraction.Ignore);
|
|
var turn = Quaternion.FromToRotation(Vector3.up, up);
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
var c = _near[i];
|
|
if (c == _cap || c.isTrigger) continue;
|
|
if (Physics.ComputePenetration(_cap, pos, turn, c, c.transform.position, c.transform.rotation, out var dir, out var dist))
|
|
pos += dir * dist;
|
|
}
|
|
}
|
|
}
|
|
}
|