| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | // The waypoint controller steers the agent to the target using direct linear guidance. |
| | 5 | | public class WaypointController : IController { |
| | 6 | | // Desired speed in m/s. |
| | 7 | | float _desiredSpeed; |
| | 8 | |
|
| 6 | 9 | | public WaypointController(Agent agent, float desiredSpeed) : base(agent) { |
| 3 | 10 | | _desiredSpeed = desiredSpeed; |
| 3 | 11 | | } |
| | 12 | |
|
| 3 | 13 | | protected override Vector3 PlanImpl(in Transformation relativeTransformation) { |
| 3 | 14 | | Vector3 toWaypoint = relativeTransformation.position.cartesian; |
| 3 | 15 | | Vector3 desiredVelocity = toWaypoint.normalized * _desiredSpeed; |
| | 16 | |
|
| | 17 | | // Calculate the acceleration needed to reach the desired velocity within one time step. |
| 3 | 18 | | Vector3 accelerationInput = |
| | 19 | | (desiredVelocity - _agent.GetVelocity()) / (float)Time.fixedDeltaTime; |
| 3 | 20 | | return accelerationInput; |
| 3 | 21 | | } |
| | 22 | | } |