This script can be used with any object that is supposed to follow a
route marked out by waypoints.
This script manages the amount to look ahead along the route,
and keeps track of progress and laps.
[SerializeField] private WaypointCircuit circuit; A reference to the waypoint-based route we should follow
The offset ahead along the route that the we will aim for
A multiplier adding distance ahead along the route to aim for, based on current speed
The offset ahead only the route for speed adjustments (applied as the rotation of the waypoint target transform)
A multiplier adding distance ahead along the route for speed adjustments
whether to update the position smoothly along the route (good for curved paths) or just when we reach each waypoint.
proximity to waypoint which must be reached to switch target to next waypoint : only used in PointToPoint mode.
these are public, readable by other objects - i.e. for an AI to know where to head!
private float progressDistance; The progress round the route, used in smooth mode.
private int progressNum; the current waypoint number, used in point-to-point mode.
private Vector3 lastPosition; Used to calculate current speed (since we may not have a rigidbody component)
private float speed; current speed of this object (calculated from delta since last frame)
setup script properties
we use a transform to represent the point to aim for, and the point which
is considered for upcoming changes-of-speed. This allows this component
to communicate this information to the AI without requiring further dependencies.
You can manually create a transform and assign it to this component *and* the AI,
then this component will update it, and the AI can read it.
reset the object to sensible values
determine the position we should currently be aiming for
(this is different to the current progress position, it is a a certain amount ahead along the route)
we use lerp as a simple way of smoothing out the speed over time.
get our current progress along the route
point to point mode. Just increase the waypoint if we're close enough:
get our current progress along the route