Here is the detailed documentation for the Waypoint System, Waypoint, and Waypoint System Editor scripts combined:
1. waypointSystem.cs
Overview:
The waypointSystem
script manages a collection of waypoints in a scene and provides utilities for pathfinding, spline creation, and waypoint manipulation. It is central to creating paths for AI vehicles to follow and includes features like shortest pathfinding, waypoint alignment, and path smoothing.
Key Variables:
- List\<waypoint> waypoints:
- Stores all the waypoints in the scene. Each waypoint is an instance of the
waypoint
class, representing a single point in the path.
- Usage: When creating a network of paths, all waypoints are stored here for easy access and manipulation.
- bool editMode:
- Toggles edit mode on/off. When enabled, it allows users to manipulate waypoints in the scene view.
- Usage: Enable this when editing waypoints to allow interaction in the scene (through the editor script).
- float handleSize (Hidden):
- Defines the size of the waypoint handles in the editor for easy selection and manipulation. It’s adjustable through the editor for user preference.
- Usage: Larger handles make selecting waypoints easier when working with a large scene or spread-out waypoints.
Functions:
1. CreateSplineFromPath(List\<waypoint> waypointList):
- This method generates a
Spline
object from a list of waypoints.
- How It Works: It iterates through the provided waypoints, creating
BezierKnot
objects for each one and adding them to the spline. Afterward, it sets the tangent mode to AutoSmooth
to ensure smooth transitions between waypoints.
- Usage Example: Useful when creating a continuous path for vehicles to follow smoothly.
2. GetNearestWaypoint(Vector3 position):
- Finds the nearest waypoint to a given position.
- How It Works: It loops through all waypoints, calculates the distance from the given position to each waypoint, and returns the nearest one.
- Usage Example: Ideal when you want to start pathfinding from the closest waypoint to an AI vehicle’s current position.
3. GetShortestPath(waypoint start, waypoint goal):