diff --git a/Assets/Scripts/Core/DemoBot.Auto.cs b/Assets/Scripts/Core/DemoBot.Auto.cs
index 285269a..ac665ba 100644
--- a/Assets/Scripts/Core/DemoBot.Auto.cs
+++ b/Assets/Scripts/Core/DemoBot.Auto.cs
@@ -130,6 +130,7 @@ namespace GrassCutter
}
}
Time.timeScale = 1f;
+ Log($"autopilot: swerved {Vehicle.Swerves}, hopped {Vehicle.Hops}");
Log("auto done");
Application.Quit();
}
diff --git a/Assets/Scripts/Gameplay/Vehicle.Auto.cs b/Assets/Scripts/Gameplay/Vehicle.Auto.cs
index ec0deb3..2da6e65 100644
--- a/Assets/Scripts/Gameplay/Vehicle.Auto.cs
+++ b/Assets/Scripts/Gameplay/Vehicle.Auto.cs
@@ -36,9 +36,49 @@ namespace GrassCutter
int _goalKey = -1;
Vector3 _unloadAt;
- /// An autopilot touches nothing: it rides on the soil and on the base deck, through everything else.
+ ///
+ /// An autopilot touches nothing, so nothing can wedge it, climb it or tip it: it rides on the soil and on the base
+ /// deck, steers round what stands in its way and goes through what it can't get round.
+ ///
const int GhostMask = ~0;
- bool _ghost;
+ /// What it steers round (not the deck rails: it rolls over those).
+ const int SolidMask = (1 << Layers.Default) | (1 << Layers.Build) | (1 << Layers.Vehicle) | (1 << Layers.Player) | (1 << Layers.LocalPlayer);
+ bool _ghost, _avoiding;
+ /// For the demo: how often an autopilot steered round something, went through it, hopped.
+ public static int Swerves, Hops;
+ static readonly RaycastHit[] s_ahead = new RaycastHit[8];
+
+ bool Solid(Collider c) => c && !c.transform.IsChildOf(transform) && !(c is BoxCollider && c.GetComponent());
+
+ /// How far ahead the way is blocked this way (MaxValue: it's clear), not counting what's near the goal.
+ float Blocked(Vector3 from, Vector3 dir, float radius, float reach, Vector3 goal)
+ {
+ int n = Physics.SphereCastNonAlloc(from, radius, dir, s_ahead, reach, SolidMask, QueryTriggerInteraction.Ignore);
+ float best = float.MaxValue;
+ for (int i = 0; i < n; i++)
+ {
+ var h = s_ahead[i];
+ if (!Solid(h.collider) || h.distance <= 0f) continue;
+ if ((h.point - goal).sqrMagnitude < 36f) continue;
+ best = Mathf.Min(best, h.distance);
+ }
+ return best;
+ }
+
+ /// Steering to take it round what stands ahead: toward the clearer side, harder the closer it is.
+ float Avoid(Vector3 p, Vector3 up, Vector3 fwd, float dist, Vector3 goal)
+ {
+ float reach = Mathf.Min(9f, dist - 1.5f);
+ if (reach < 2f) return 0f;
+ var from = p + up * 1.1f;
+ float r = Mathf.Max(0.6f, width * 0.5f);
+ float ahead = Blocked(from, fwd, r, reach, goal);
+ if (ahead == float.MaxValue) return 0f;
+ float left = Blocked(from, Quaternion.AngleAxis(-40f, up) * fwd, r, reach, goal);
+ float right = Blocked(from, Quaternion.AngleAxis(40f, up) * fwd, r, reach, goal);
+ float side = left > right ? -1f : left < right ? 1f : (Vector3.SignedAngle(fwd, goal - p, up) < 0f ? -1f : 1f);
+ return side * Mathf.Lerp(1.4f, 0.5f, ahead / reach);
+ }
static readonly RaycastHit[] s_deckHits = new RaycastHit[8];
/// How high this wheel stands over the base deck's slab under it, if there is one (it keeps to the deck, not to what's on it).
@@ -68,6 +108,7 @@ namespace GrassCutter
float d = to.magnitude;
if (d < 0.5f) return;
var dir = to / d;
+ Hops++;
var at = Planet.Surface(p + dir * Mathf.Min(metres, d));
var nu = Planet.Up(at);
// onto the base deck, not under it
@@ -318,17 +359,23 @@ namespace GrassCutter
if (dist < 6f) thr = Mathf.Min(thr, 0.3f + dist * 0.08f);
// a sharp turn close by is taken slowly: the slower it goes, the tighter it turns
if (Mathf.Abs(ang) > 50f && dist < 10f) thr = Mathf.Min(thr, 0.22f);
+ // round what stands in the way; what it can't get round it goes through
+ float avoid = Avoid(p, up, fwd, dist, goal);
+ if (avoid == 0f) _avoiding = false;
+ else
+ {
+ if (!_avoiding) Swerves++;
+ _avoiding = true;
+ steer = Mathf.Clamp(steer + avoid, -1f, 1f);
+ thr = Mathf.Min(thr, 0.5f);
+ }
_lastSteer = steer;
- // it drives through what's in its way; still not moving (wedged on a slope, sunk in something), it hops on
+ // nothing stops it, so not moving means wedged on the ground somehow: it hops on
if (speed < 0.5f && thr > 0.2f)
{
_stuck += dt;
- if (_stuck > 2f)
- {
- _stuck = 0f;
- Hop(goal, 4f);
- }
+ if (_stuck > 3f) { _stuck = 0f; Hop(goal, 4f); }
}
else _stuck = 0f;
return new Vector2(steer, thr);
diff --git a/Assets/Scripts/Gameplay/Vehicle.cs b/Assets/Scripts/Gameplay/Vehicle.cs
index f8e2684..c5b9c16 100644
--- a/Assets/Scripts/Gameplay/Vehicle.cs
+++ b/Assets/Scripts/Gameplay/Vehicle.cs
@@ -295,7 +295,7 @@ namespace GrassCutter
var up = Planet.Up(transform.position);
Body.AddForce(-up * GameDefs.LowGravity, ForceMode.Acceleration);
- // on autopilot nothing stops it: it drives through buildings, rocks, props, people and machines, on the soil
+ // on autopilot it touches nothing: it steers round things and goes through what it can't get round
Ghost(auto);
// raycast springs at four corners (soil, deck, ramp)
@@ -308,7 +308,7 @@ namespace GrassCutter
var local = new Vector3(Probes[i].x * width, 0f, Probes[i].y * length);
var wp = transform.TransformPoint(local);
float clearance = Planet.Altitude(wp);
- if (auto) clearance = Mathf.Min(clearance, DeckClearance(wp, up, 1.2f + ride + 1.5f));
+ if (_ghost) clearance = Mathf.Min(clearance, DeckClearance(wp, up, 1.2f + ride + 1.5f));
else if (Physics.Raycast(wp + up * 1.2f, -up, out var sh, 1.2f + ride + 1.5f, groundMask, QueryTriggerInteraction.Ignore))
clearance = Mathf.Min(clearance, sh.distance - 1.2f);
float depth = ride - clearance;