IAstarAI.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using UnityEngine;
  2. namespace Pathfinding {
  3. /// <summary>
  4. /// Common interface for all movement scripts in the A* Pathfinding Project.
  5. /// See: <see cref="Pathfinding.AIPath"/>
  6. /// See: <see cref="Pathfinding.RichAI"/>
  7. /// See: <see cref="Pathfinding.AILerp"/>
  8. /// </summary>
  9. public interface IAstarAI {
  10. /// <summary>
  11. /// Height of the agent in world units.
  12. /// This is visualized in the scene view as a yellow cylinder around the character.
  13. ///
  14. /// This value is currently only used if an RVOController is attached to the same GameObject, otherwise it is only used for drawing nice gizmos in the scene view.
  15. /// However since the height value is used for some things, the radius field is always visible for consistency and easier visualization of the character.
  16. /// That said, it may be used for something in a future release.
  17. ///
  18. /// Note: The <see cref="Pathfinding.AILerp"/> script doesn't really have any use of knowing the radius or the height of the character, so this property will always return 0 in that script.
  19. /// </summary>
  20. float radius { get; set; }
  21. /// <summary>
  22. /// Radius of the agent in world units.
  23. /// This is visualized in the scene view as a yellow cylinder around the character.
  24. ///
  25. /// Note: The <see cref="Pathfinding.AILerp"/> script doesn't really have any use of knowing the radius or the height of the character, so this property will always return 0 in that script.
  26. /// </summary>
  27. float height { get; set; }
  28. /// <summary>
  29. /// Position of the agent.
  30. /// In world space.
  31. /// See: <see cref="rotation"/>
  32. /// </summary>
  33. Vector3 position { get; }
  34. /// <summary>
  35. /// Rotation of the agent.
  36. /// In world space.
  37. /// See: <see cref="position"/>
  38. /// </summary>
  39. Quaternion rotation { get; }
  40. /// <summary>Max speed in world units per second</summary>
  41. float maxSpeed { get; set; }
  42. /// <summary>
  43. /// Actual velocity that the agent is moving with.
  44. /// In world units per second.
  45. ///
  46. /// See: <see cref="desiredVelocity"/>
  47. /// </summary>
  48. Vector3 velocity { get; }
  49. /// <summary>
  50. /// Velocity that this agent wants to move with.
  51. /// Includes gravity and local avoidance if applicable.
  52. /// In world units per second.
  53. ///
  54. /// See: <see cref="velocity"/>
  55. /// </summary>
  56. Vector3 desiredVelocity { get; }
  57. /// <summary>
  58. /// Remaining distance along the current path to the end of the path.
  59. /// For the RichAI movement script this may not always be precisely known, especially when
  60. /// far away from the destination. In those cases an approximate distance will be returned.
  61. ///
  62. /// If the agent does not currently have a path, then positive infinity will be returned.
  63. ///
  64. /// Note: This is the distance to the end of the path, which may or may not be at the <see cref="destination"/>. If the character cannot reach the destination it will try to move as close as possible to it.
  65. ///
  66. /// Warning: Since path requests are asynchronous, there is a small delay between a path request being sent and this value being updated with the new calculated path.
  67. ///
  68. /// See: <see cref="reachedDestination"/>
  69. /// See: <see cref="reachedEndOfPath"/>
  70. /// </summary>
  71. float remainingDistance { get; }
  72. /// <summary>
  73. /// True if the ai has reached the <see cref="destination"/>.
  74. /// This is a best effort calculation to see if the <see cref="destination"/> has been reached.
  75. /// For the AIPath/RichAI scripts, this is when the character is within <see cref="AIPath.endReachedDistance"/> world units from the <see cref="destination"/>.
  76. /// For the AILerp script it is when the character is at the destination (±a very small margin).
  77. ///
  78. /// This value will be updated immediately when the <see cref="destination"/> is changed (in contrast to <see cref="reachedEndOfPath)"/>, however since path requests are asynchronous
  79. /// it will use an approximation until it sees the real path result. What this property does is to check the distance to the end of the current path, and add to that the distance
  80. /// from the end of the path to the <see cref="destination"/> (i.e. is assumes it is possible to move in a straight line between the end of the current path to the destination) and then checks if that total
  81. /// distance is less than <see cref="endReachedDistance"/>. This property is therefore only a best effort, but it will work well for almost all use cases.
  82. ///
  83. /// Furthermore it will not report that the destination is reached if the destination is above the head of the character or more than half the <see cref="height"/> of the character below its feet
  84. /// (so if you have a multilevel building, it is important that you configure the <see cref="height"/> of the character correctly).
  85. ///
  86. /// The cases which could be problematic are if an agent is standing next to a very thin wall and the destination suddenly changes to the other side of that thin wall.
  87. /// During the time that it takes for the path to be calculated the agent may see itself as alredy having reached the destination because the destination only moved a very small distance (the wall was thin),
  88. /// even though it may actually be quite a long way around the wall to the other side.
  89. ///
  90. /// In contrast to <see cref="reachedEndOfPath"/>, this property is immediately updated when the <see cref="destination"/> is changed.
  91. ///
  92. /// See: <see cref="AIPath.endReachedDistance"/>
  93. /// See: <see cref="remainingDistance"/>
  94. /// See: <see cref="reachedEndOfPath"/>
  95. /// </summary>
  96. bool reachedDestination { get; }
  97. /// <summary>
  98. /// True if the agent has reached the end of the current path.
  99. ///
  100. /// Note that setting the <see cref="destination"/> does not immediately update the path, nor is there any guarantee that the
  101. /// AI will actually be able to reach the destination that you set. The AI will try to get as close as possible.
  102. /// Often you want to use <see cref="reachedDestination"/> instead which is easier to work with.
  103. ///
  104. /// It is very hard to provide a method for detecting if the AI has reached the <see cref="destination"/> that works across all different games
  105. /// because the destination may not even lie on the navmesh and how that is handled differs from game to game (see also the code snippet in the docs for <see cref="destination)"/>.
  106. ///
  107. /// See: <see cref="remainingDistance"/>
  108. /// See: <see cref="reachedDestination"/>
  109. /// </summary>
  110. bool reachedEndOfPath { get; }
  111. /// <summary>
  112. /// Position in the world that this agent should move to.
  113. ///
  114. /// If no destination has been set yet, then (+infinity, +infinity, +infinity) will be returned.
  115. ///
  116. /// Note that setting this property does not immediately cause the agent to recalculate its path.
  117. /// So it may take some time before the agent starts to move towards this point.
  118. /// Most movement scripts have a repathRate field which indicates how often the agent looks
  119. /// for a new path. You can also call the <see cref="SearchPath"/> method to immediately
  120. /// start to search for a new path. Paths are calculated asynchronously so when an agent starts to
  121. /// search for path it may take a few frames (usually 1 or 2) until the result is available.
  122. /// During this time the <see cref="pathPending"/> property will return true.
  123. ///
  124. /// If you are setting a destination and then want to know when the agent has reached that destination
  125. /// then you should check both <see cref="pathPending"/> and <see cref="reachedEndOfPath"/>:
  126. /// <code>
  127. /// IEnumerator Start () {
  128. /// ai.destination = somePoint;
  129. /// // Start to search for a path to the destination immediately
  130. /// // Note that the result may not become available until after a few frames
  131. /// // ai.pathPending will be true while the path is being calculated
  132. /// ai.SearchPath();
  133. /// // Wait until we know for sure that the agent has calculated a path to the destination we set above
  134. /// while (ai.pathPending || !ai.reachedEndOfPath) {
  135. /// yield return null;
  136. /// }
  137. /// // The agent has reached the destination now
  138. /// }
  139. /// </code>
  140. /// </summary>
  141. Vector3 destination { get; set; }
  142. /// <summary>
  143. /// Enables or disables recalculating the path at regular intervals.
  144. /// Setting this to false does not stop any active path requests from being calculated or stop it from continuing to follow the current path.
  145. ///
  146. /// Note that this only disables automatic path recalculations. If you call the <see cref="SearchPath()"/> method a path will still be calculated.
  147. ///
  148. /// See: <see cref="canMove"/>
  149. /// See: <see cref="isStopped"/>
  150. /// </summary>
  151. bool canSearch { get; set; }
  152. /// <summary>
  153. /// Enables or disables movement completely.
  154. /// If you want the agent to stand still, but still react to local avoidance and use gravity: use <see cref="isStopped"/> instead.
  155. ///
  156. /// This is also useful if you want to have full control over when the movement calculations run.
  157. /// Take a look at <see cref="MovementUpdate"/>
  158. ///
  159. /// See: <see cref="canSearch"/>
  160. /// See: <see cref="isStopped"/>
  161. /// </summary>
  162. bool canMove { get; set; }
  163. /// <summary>True if this agent currently has a path that it follows</summary>
  164. bool hasPath { get; }
  165. /// <summary>True if a path is currently being calculated</summary>
  166. bool pathPending { get; }
  167. /// <summary>
  168. /// Gets or sets if the agent should stop moving.
  169. /// If this is set to true the agent will immediately start to slow down as quickly as it can to come to a full stop.
  170. /// The agent will still react to local avoidance and gravity (if applicable), but it will not try to move in any particular direction.
  171. ///
  172. /// The current path of the agent will not be cleared, so when this is set
  173. /// to false again the agent will continue moving along the previous path.
  174. ///
  175. /// This is a purely user-controlled parameter, so for example it is not set automatically when the agent stops
  176. /// moving because it has reached the target. Use <see cref="reachedEndOfPath"/> for that.
  177. ///
  178. /// If this property is set to true while the agent is traversing an off-mesh link (RichAI script only), then the agent will
  179. /// continue traversing the link and stop once it has completed it.
  180. ///
  181. /// Note: This is not the same as the <see cref="canMove"/> setting which some movement scripts have. The <see cref="canMove"/> setting
  182. /// disables movement calculations completely (which among other things makes it not be affected by local avoidance or gravity).
  183. /// For the AILerp movement script which doesn't use gravity or local avoidance anyway changing this property is very similar to
  184. /// changing <see cref="canMove"/>.
  185. ///
  186. /// The <see cref="steeringTarget"/> property will continue to indicate the point which the agent would move towards if it would not be stopped.
  187. /// </summary>
  188. bool isStopped { get; set; }
  189. /// <summary>
  190. /// Point on the path which the agent is currently moving towards.
  191. /// This is usually a point a small distance ahead of the agent
  192. /// or the end of the path.
  193. ///
  194. /// If the agent does not have a path at the moment, then the agent's current position will be returned.
  195. /// </summary>
  196. Vector3 steeringTarget { get; }
  197. /// <summary>
  198. /// Called when the agent recalculates its path.
  199. /// This is called both for automatic path recalculations (see <see cref="canSearch)"/> and manual ones (see <see cref="SearchPath)"/>.
  200. ///
  201. /// See: Take a look at the <see cref="Pathfinding.AIDestinationSetter"/> source code for an example of how it can be used.
  202. /// </summary>
  203. System.Action onSearchPath { get; set; }
  204. /// <summary>
  205. /// Recalculate the current path.
  206. /// You can for example use this if you want very quick reaction times when you have changed the <see cref="destination"/>
  207. /// so that the agent does not have to wait until the next automatic path recalculation (see <see cref="canSearch)"/>.
  208. ///
  209. /// If there is an ongoing path calculation, it will be canceled, so make sure you leave time for the paths to get calculated before calling this function again.
  210. /// A canceled path will show up in the log with the message "Canceled by script" (see <see cref="Seeker.CancelCurrentPathRequest())"/>.
  211. ///
  212. /// If no <see cref="destination"/> has been set yet then nothing will be done.
  213. ///
  214. /// Note: The path result may not become available until after a few frames.
  215. /// During the calculation time the <see cref="pathPending"/> property will return true.
  216. ///
  217. /// See: <see cref="pathPending"/>
  218. /// </summary>
  219. void SearchPath ();
  220. /// <summary>
  221. /// Make the AI follow the specified path.
  222. /// In case the path has not been calculated, the script will call seeker.StartPath to calculate it.
  223. /// This means the AI may not actually start to follow the path until in a few frames when the path has been calculated.
  224. /// The <see cref="pathPending"/> field will as usual return true while the path is being calculated.
  225. ///
  226. /// In case the path has already been calculated it will immediately replace the current path the AI is following.
  227. /// This is useful if you want to replace how the AI calculates its paths.
  228. /// Note that if you calculate the path using seeker.StartPath then this script will already pick it up because it is listening for
  229. /// all paths that the Seeker finishes calculating. In that case you do not need to call this function.
  230. ///
  231. /// You can disable the automatic path recalculation by setting the <see cref="canSearch"/> field to false.
  232. ///
  233. /// <code>
  234. /// // Disable the automatic path recalculation
  235. /// ai.canSearch = false;
  236. /// var pointToAvoid = enemy.position;
  237. /// // Make the AI flee from the enemy.
  238. /// // The path will be about 20 world units long (the default cost of moving 1 world unit is 1000).
  239. /// var path = FleePath.Construct(ai.position, pointToAvoid, 1000 * 20);
  240. /// ai.SetPath(path);
  241. /// </code>
  242. /// </summary>
  243. void SetPath (Path path);
  244. /// <summary>
  245. /// Instantly move the agent to a new position.
  246. /// This will trigger a path recalculation (if clearPath is true, which is the default) so if you want to teleport the agent and change its <see cref="destination"/>
  247. /// it is recommended that you set the <see cref="destination"/> before calling this method.
  248. ///
  249. /// The current path will be cleared by default.
  250. ///
  251. /// See: Works similarly to Unity's NavmeshAgent.Warp.
  252. /// See: <see cref="SearchPath"/>
  253. /// </summary>
  254. void Teleport (Vector3 newPosition, bool clearPath = true);
  255. /// <summary>
  256. /// Move the agent.
  257. ///
  258. /// This is intended for external movement forces such as those applied by wind, conveyor belts, knockbacks etc.
  259. ///
  260. /// Some movement scripts may ignore this completely (notably the AILerp script) if it does not have
  261. /// any concept of being moved externally.
  262. ///
  263. /// The agent will not be moved immediately when calling this method. Instead this offset will be stored and then
  264. /// applied the next time the agent runs its movement calculations (which is usually later this frame or the next frame).
  265. /// If you want to move the agent immediately then call:
  266. /// <code>
  267. /// ai.Move(someVector);
  268. /// ai.FinalizeMovement(ai.position, ai.rotation);
  269. /// </code>
  270. /// </summary>
  271. /// <param name="deltaPosition">Direction and distance to move the agent in world space.</param>
  272. void Move (Vector3 deltaPosition);
  273. /// <summary>
  274. /// Calculate how the character wants to move during this frame.
  275. ///
  276. /// Note that this does not actually move the character. You need to call <see cref="FinalizeMovement"/> for that.
  277. /// This is called automatically unless <see cref="canMove"/> is false.
  278. ///
  279. /// To handle movement yourself you can disable <see cref="canMove"/> and call this method manually.
  280. /// This code will replicate the normal behavior of the component:
  281. /// <code>
  282. /// void Update () {
  283. /// // Disable the AIs own movement code
  284. /// ai.canMove = false;
  285. /// Vector3 nextPosition;
  286. /// Quaternion nextRotation;
  287. /// // Calculate how the AI wants to move
  288. /// ai.MovementUpdate(Time.deltaTime, out nextPosition, out nextRotation);
  289. /// // Modify nextPosition and nextRotation in any way you wish
  290. /// // Actually move the AI
  291. /// ai.FinalizeMovement(nextPosition, nextRotation);
  292. /// }
  293. /// </code>
  294. /// </summary>
  295. /// <param name="deltaTime">time to simulate movement for. Usually set to Time.deltaTime.</param>
  296. /// <param name="nextPosition">the position that the agent wants to move to during this frame.</param>
  297. /// <param name="nextRotation">the rotation that the agent wants to rotate to during this frame.</param>
  298. void MovementUpdate (float deltaTime, out Vector3 nextPosition, out Quaternion nextRotation);
  299. /// <summary>
  300. /// Move the agent.
  301. /// To be called as the last step when you are handling movement manually.
  302. ///
  303. /// The movement will be clamped to the navmesh if applicable (this is done for the RichAI movement script).
  304. ///
  305. /// See: <see cref="MovementUpdate"/> for a code example.
  306. /// </summary>
  307. void FinalizeMovement (Vector3 nextPosition, Quaternion nextRotation);
  308. }
  309. }