using UnityEngine; namespace Pathfinding { /// /// Common interface for all movement scripts in the A* Pathfinding Project. /// See: /// See: /// See: /// public interface IAstarAI { /// /// Height of the agent in world units. /// This is visualized in the scene view as a yellow cylinder around the character. /// /// 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. /// However since the height value is used for some things, the radius field is always visible for consistency and easier visualization of the character. /// That said, it may be used for something in a future release. /// /// Note: The 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. /// float radius { get; set; } /// /// Radius of the agent in world units. /// This is visualized in the scene view as a yellow cylinder around the character. /// /// Note: The 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. /// float height { get; set; } /// /// Position of the agent. /// In world space. /// See: /// Vector3 position { get; } /// /// Rotation of the agent. /// In world space. /// See: /// Quaternion rotation { get; } /// Max speed in world units per second float maxSpeed { get; set; } /// /// Actual velocity that the agent is moving with. /// In world units per second. /// /// See: /// Vector3 velocity { get; } /// /// Velocity that this agent wants to move with. /// Includes gravity and local avoidance if applicable. /// In world units per second. /// /// See: /// Vector3 desiredVelocity { get; } /// /// Remaining distance along the current path to the end of the path. /// For the RichAI movement script this may not always be precisely known, especially when /// far away from the destination. In those cases an approximate distance will be returned. /// /// If the agent does not currently have a path, then positive infinity will be returned. /// /// Note: This is the distance to the end of the path, which may or may not be at the . If the character cannot reach the destination it will try to move as close as possible to it. /// /// 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. /// /// See: /// See: /// float remainingDistance { get; } /// /// True if the ai has reached the . /// This is a best effort calculation to see if the has been reached. /// For the AIPath/RichAI scripts, this is when the character is within world units from the . /// For the AILerp script it is when the character is at the destination (±a very small margin). /// /// This value will be updated immediately when the is changed (in contrast to , however since path requests are asynchronous /// 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 /// from the end of the path to the (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 /// distance is less than . This property is therefore only a best effort, but it will work well for almost all use cases. /// /// 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 of the character below its feet /// (so if you have a multilevel building, it is important that you configure the of the character correctly). /// /// 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. /// 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), /// even though it may actually be quite a long way around the wall to the other side. /// /// In contrast to , this property is immediately updated when the is changed. /// /// See: /// See: /// See: /// bool reachedDestination { get; } /// /// True if the agent has reached the end of the current path. /// /// Note that setting the does not immediately update the path, nor is there any guarantee that the /// AI will actually be able to reach the destination that you set. The AI will try to get as close as possible. /// Often you want to use instead which is easier to work with. /// /// It is very hard to provide a method for detecting if the AI has reached the that works across all different games /// 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: /// See: /// bool reachedEndOfPath { get; } /// /// Position in the world that this agent should move to. /// /// If no destination has been set yet, then (+infinity, +infinity, +infinity) will be returned. /// /// Note that setting this property does not immediately cause the agent to recalculate its path. /// So it may take some time before the agent starts to move towards this point. /// Most movement scripts have a repathRate field which indicates how often the agent looks /// for a new path. You can also call the method to immediately /// start to search for a new path. Paths are calculated asynchronously so when an agent starts to /// search for path it may take a few frames (usually 1 or 2) until the result is available. /// During this time the property will return true. /// /// If you are setting a destination and then want to know when the agent has reached that destination /// then you should check both and : /// /// IEnumerator Start () { /// ai.destination = somePoint; /// // Start to search for a path to the destination immediately /// // Note that the result may not become available until after a few frames /// // ai.pathPending will be true while the path is being calculated /// ai.SearchPath(); /// // Wait until we know for sure that the agent has calculated a path to the destination we set above /// while (ai.pathPending || !ai.reachedEndOfPath) { /// yield return null; /// } /// // The agent has reached the destination now /// } /// /// Vector3 destination { get; set; } /// /// Enables or disables recalculating the path at regular intervals. /// Setting this to false does not stop any active path requests from being calculated or stop it from continuing to follow the current path. /// /// Note that this only disables automatic path recalculations. If you call the method a path will still be calculated. /// /// See: /// See: /// bool canSearch { get; set; } /// /// Enables or disables movement completely. /// If you want the agent to stand still, but still react to local avoidance and use gravity: use instead. /// /// This is also useful if you want to have full control over when the movement calculations run. /// Take a look at /// /// See: /// See: /// bool canMove { get; set; } /// True if this agent currently has a path that it follows bool hasPath { get; } /// True if a path is currently being calculated bool pathPending { get; } /// /// Gets or sets if the agent should stop moving. /// 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. /// The agent will still react to local avoidance and gravity (if applicable), but it will not try to move in any particular direction. /// /// The current path of the agent will not be cleared, so when this is set /// to false again the agent will continue moving along the previous path. /// /// This is a purely user-controlled parameter, so for example it is not set automatically when the agent stops /// moving because it has reached the target. Use for that. /// /// If this property is set to true while the agent is traversing an off-mesh link (RichAI script only), then the agent will /// continue traversing the link and stop once it has completed it. /// /// Note: This is not the same as the setting which some movement scripts have. The setting /// disables movement calculations completely (which among other things makes it not be affected by local avoidance or gravity). /// For the AILerp movement script which doesn't use gravity or local avoidance anyway changing this property is very similar to /// changing . /// /// The property will continue to indicate the point which the agent would move towards if it would not be stopped. /// bool isStopped { get; set; } /// /// Point on the path which the agent is currently moving towards. /// This is usually a point a small distance ahead of the agent /// or the end of the path. /// /// If the agent does not have a path at the moment, then the agent's current position will be returned. /// Vector3 steeringTarget { get; } /// /// Called when the agent recalculates its path. /// This is called both for automatic path recalculations (see and manual ones (see . /// /// See: Take a look at the source code for an example of how it can be used. /// System.Action onSearchPath { get; set; } /// /// Recalculate the current path. /// You can for example use this if you want very quick reaction times when you have changed the /// so that the agent does not have to wait until the next automatic path recalculation (see . /// /// 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. /// A canceled path will show up in the log with the message "Canceled by script" (see . /// /// If no has been set yet then nothing will be done. /// /// Note: The path result may not become available until after a few frames. /// During the calculation time the property will return true. /// /// See: /// void SearchPath (); /// /// Make the AI follow the specified path. /// In case the path has not been calculated, the script will call seeker.StartPath to calculate it. /// This means the AI may not actually start to follow the path until in a few frames when the path has been calculated. /// The field will as usual return true while the path is being calculated. /// /// In case the path has already been calculated it will immediately replace the current path the AI is following. /// This is useful if you want to replace how the AI calculates its paths. /// Note that if you calculate the path using seeker.StartPath then this script will already pick it up because it is listening for /// all paths that the Seeker finishes calculating. In that case you do not need to call this function. /// /// You can disable the automatic path recalculation by setting the field to false. /// /// /// // Disable the automatic path recalculation /// ai.canSearch = false; /// var pointToAvoid = enemy.position; /// // Make the AI flee from the enemy. /// // The path will be about 20 world units long (the default cost of moving 1 world unit is 1000). /// var path = FleePath.Construct(ai.position, pointToAvoid, 1000 * 20); /// ai.SetPath(path); /// /// void SetPath (Path path); /// /// Instantly move the agent to a new position. /// 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 /// it is recommended that you set the before calling this method. /// /// The current path will be cleared by default. /// /// See: Works similarly to Unity's NavmeshAgent.Warp. /// See: /// void Teleport (Vector3 newPosition, bool clearPath = true); /// /// Move the agent. /// /// This is intended for external movement forces such as those applied by wind, conveyor belts, knockbacks etc. /// /// Some movement scripts may ignore this completely (notably the AILerp script) if it does not have /// any concept of being moved externally. /// /// The agent will not be moved immediately when calling this method. Instead this offset will be stored and then /// applied the next time the agent runs its movement calculations (which is usually later this frame or the next frame). /// If you want to move the agent immediately then call: /// /// ai.Move(someVector); /// ai.FinalizeMovement(ai.position, ai.rotation); /// /// /// Direction and distance to move the agent in world space. void Move (Vector3 deltaPosition); /// /// Calculate how the character wants to move during this frame. /// /// Note that this does not actually move the character. You need to call for that. /// This is called automatically unless is false. /// /// To handle movement yourself you can disable and call this method manually. /// This code will replicate the normal behavior of the component: /// /// void Update () { /// // Disable the AIs own movement code /// ai.canMove = false; /// Vector3 nextPosition; /// Quaternion nextRotation; /// // Calculate how the AI wants to move /// ai.MovementUpdate(Time.deltaTime, out nextPosition, out nextRotation); /// // Modify nextPosition and nextRotation in any way you wish /// // Actually move the AI /// ai.FinalizeMovement(nextPosition, nextRotation); /// } /// /// /// time to simulate movement for. Usually set to Time.deltaTime. /// the position that the agent wants to move to during this frame. /// the rotation that the agent wants to rotate to during this frame. void MovementUpdate (float deltaTime, out Vector3 nextPosition, out Quaternion nextRotation); /// /// Move the agent. /// To be called as the last step when you are handling movement manually. /// /// The movement will be clamped to the navmesh if applicable (this is done for the RichAI movement script). /// /// See: for a code example. /// void FinalizeMovement (Vector3 nextPosition, Quaternion nextRotation); } }