Difference between revisions of "PathEnemyFSM"
(Updating API info) |
(Updating API info) |
||
(One intermediate revision by one user not shown) | |||
Line 6: | Line 6: | ||
=== playerTag : string === | === playerTag : string === | ||
If the player object is not set when the scene starts, this script will attempt to find an object with this tag. If no object is found, an error is printed to the debug console. | If the player object is not set when the scene starts, this script will attempt to find an object with this tag. If no object is found, an error is printed to the debug console. | ||
− | === path: Transform&91;&93; === | + | === path: Transform[] === |
This is the set of nodes that make up the path that will be followed. The enemy moves to the first node in this list before continuing along the path in the direction specified by the other variables below. | This is the set of nodes that make up the path that will be followed. The enemy moves to the first node in this list before continuing along the path in the direction specified by the other variables below. | ||
− | + | === howToLoop : PathEnemyLoopStyle === | |
− | + | This tells the enemy how to proceed once the end (or beginning) of the list is reached. | |
− | + | ==== None ==== | |
− | + | This will cause the enemy to stop moving and just stay at the end of the path, unless the player comes within range. | |
− | + | ==== Loop ==== | |
− | + | This will cause the enemy to continue on in the same direction. | |
− | + | ==== PingPong ==== | |
− | + | This will cause the enemy to switch directions and go back the way it came. | |
− | + | === currentDirection : PathEnemyPathDirection === | |
− | + | This is the direction along the path the enemy is going. This relates to how the enemy traverses the path list, not necessarily how the enemy is moving in the environment. This is used in conjunction with the pathStep to determine the next waypoint when a new one is needed. It may have the following values: | |
− | + | ==== None ==== | |
− | + | This causes the enemy to stop moving along the path completely. It will chase the player if they pass within range, however. | |
− | + | ==== Forward ==== | |
− | + | This causes the enemy to step forward through the Transforms in the path list, starting with 0 and incrementing by the pathStep at each waypoint. | |
− | + | ==== Backward ==== | |
− | + | This causes the enemy to step backward through the Transforms in the path list, starting with 0 and decrementing by the pathStep at each waypoint. | |
− | + | === pathStep : int === | |
− | + | This is how many nodes down the path to move each time it needs to update the current node. The absolute value of this value is used (positive and negative should be handled by the currentDirection). | |
− | + | === chaseStartDistance : float === | |
− | + | This is how close the player must be before the enemy will start chasing. | |
− | + | === chaseStopDistance : float === | |
− | + | This is how far away the player must be before the enemy will stop chasing. | |
− | + | === movementSpeed : float === | |
− | + | This is how fast the enemy moves. | |
− | + | === rotateToPath : bool === | |
− | + | Whether or not the enemy rotates when moving to face the next node in the path. | |
− | + | === rotateSpeed : float === | |
− | + | This is how fast the enemy will rotate towards whatever it's moving towards. | |
− | + | === chaseMode : PathEnemyChaseMode === | |
− | + | This is how the enemy will move along its path and after the player. Possible values are: | |
− | + | ==== Lerp ==== | |
− | + | This uses [http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html Vector3.Lerp] to move from the current position to the target position. This chaseMode doesn't require a rigidbody. It has the effect of moving slower the closer it gets to the target. | |
− | + | ==== SetVelocity ==== | |
− | + | This requires a [http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.html rigidbody] be attached to the enemy. When the scene starts, a rigidbody will be added to the enemy if this mode is selected, and one is not present. To move, the rigidbody's [http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-velocity.html velocity] is set directly. | |
− | + | ==== AddForce ==== | |
− | + | This also requires a [http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.html rigidbody] be attached to the enemy. When the scene starts, a rigidbody will be added to the enemy if this mode is selected, and one is not present. To move, [http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html AddForce] is called in the direction of the target object. '''WARNING''': This chase mode has a tendency to cause the enemy to [http://en.wikipedia.org/wiki/Orbit orbit] the target object (player or waypoint). | |
− | + | === waypointRadius : float === | |
− | + | This is how close the enemy must get to the waypoints along the path before transitioning to the next waypoint. | |
− | + | === leashDistance : float === | |
− | + | This is how far away the enemy can get from the current waypoint while chasing the player before turning back, if useLeash is set to true. | |
− | + | === useLeash : bool === | |
− | + | This indicates whether or not the leashDistance should be enforced. | |
− | + | === is2dScene : bool === | |
− | + | This flag indicates whether or not the scene is 2d or 3d. This affects how the rotation is calculated when the enemy is rotating to face an object. | |
− | + | === showGizmos : bool === | |
− | + | This flag will enable or disable gizmos drawn in the scene window of the editor. Gizmos are drawn when this is set to true, and the enemy is selected in the hierarchy. This flag is only available in the editor. | |
− | + | ||
− | + | The gizmos for this script have the following meanings: | |
− | + | ||
− | + | '''Green''': This is the radius at which the enemy will start chasing the player (chaseStartDistance). | |
− | + | ||
− | + | '''Red''': This is the radius at which the enemy will stop chasing (chaseStopDistance). | |
− | + | ||
− | + | '''Yellow''': These are the waypoints on the path. The radius of these nodes is the waypointRadius. | |
− | + | ||
+ | '''Magenta''': This is the current waypoint. The radius of this node is also the waypointRadius. | ||
+ | |||
+ | '''Blue''': This shows the leashDistance for each node on the path. | ||
+ | |||
+ | == Public Functions == | ||
+ | These are used by the [[PathEnemyStates]] to implement the movement and behavior of the enemy. | ||
+ | === bool CanMove === | ||
+ | This checks if the enemy can currently move or not. | ||
+ | === Transform GetCurrentWaypoint () === | ||
+ | This returns the [http://docs.unity3d.com/Documentation/ScriptReference/Transform.html Transform] for the current waypoint along the path. | ||
+ | === void UpdateCurrentWaypoint () === | ||
+ | This is checks the distance between the enemy and the next waypoint; if the enemy is close enough to the waypoint (within waypointRadius), the current waypoint is updated according to the settings for currentDirection, pathStep, and howToLoop. | ||
Latest revision as of 20:19, 16 April 2013
This script implements an AI that will follow a set path, unless the player comes to close. Once the player is close enough it will follow the player until the player is out of range, or until the maximum distance away from the path is reached (if useLeash is set to true).
[edit] Public Variables
[edit] player : GameObject
This is the player object that the enemy will chase if it gets close enough.
[edit] playerTag : string
If the player object is not set when the scene starts, this script will attempt to find an object with this tag. If no object is found, an error is printed to the debug console.
[edit] path: Transform[]
This is the set of nodes that make up the path that will be followed. The enemy moves to the first node in this list before continuing along the path in the direction specified by the other variables below.
[edit] howToLoop : PathEnemyLoopStyle
This tells the enemy how to proceed once the end (or beginning) of the list is reached.
[edit] None
This will cause the enemy to stop moving and just stay at the end of the path, unless the player comes within range.
[edit] Loop
This will cause the enemy to continue on in the same direction.
[edit] PingPong
This will cause the enemy to switch directions and go back the way it came.
[edit] currentDirection : PathEnemyPathDirection
This is the direction along the path the enemy is going. This relates to how the enemy traverses the path list, not necessarily how the enemy is moving in the environment. This is used in conjunction with the pathStep to determine the next waypoint when a new one is needed. It may have the following values:
[edit] None
This causes the enemy to stop moving along the path completely. It will chase the player if they pass within range, however.
[edit] Forward
This causes the enemy to step forward through the Transforms in the path list, starting with 0 and incrementing by the pathStep at each waypoint.
[edit] Backward
This causes the enemy to step backward through the Transforms in the path list, starting with 0 and decrementing by the pathStep at each waypoint.
[edit] pathStep : int
This is how many nodes down the path to move each time it needs to update the current node. The absolute value of this value is used (positive and negative should be handled by the currentDirection).
[edit] chaseStartDistance : float
This is how close the player must be before the enemy will start chasing.
[edit] chaseStopDistance : float
This is how far away the player must be before the enemy will stop chasing.
[edit] movementSpeed : float
This is how fast the enemy moves.
[edit] rotateToPath : bool
Whether or not the enemy rotates when moving to face the next node in the path.
[edit] rotateSpeed : float
This is how fast the enemy will rotate towards whatever it's moving towards.
[edit] chaseMode : PathEnemyChaseMode
This is how the enemy will move along its path and after the player. Possible values are:
[edit] Lerp
This uses Vector3.Lerp to move from the current position to the target position. This chaseMode doesn't require a rigidbody. It has the effect of moving slower the closer it gets to the target.
[edit] SetVelocity
This requires a rigidbody be attached to the enemy. When the scene starts, a rigidbody will be added to the enemy if this mode is selected, and one is not present. To move, the rigidbody's velocity is set directly.
[edit] AddForce
This also requires a rigidbody be attached to the enemy. When the scene starts, a rigidbody will be added to the enemy if this mode is selected, and one is not present. To move, AddForce is called in the direction of the target object. WARNING: This chase mode has a tendency to cause the enemy to orbit the target object (player or waypoint).
[edit] waypointRadius : float
This is how close the enemy must get to the waypoints along the path before transitioning to the next waypoint.
[edit] leashDistance : float
This is how far away the enemy can get from the current waypoint while chasing the player before turning back, if useLeash is set to true.
[edit] useLeash : bool
This indicates whether or not the leashDistance should be enforced.
[edit] is2dScene : bool
This flag indicates whether or not the scene is 2d or 3d. This affects how the rotation is calculated when the enemy is rotating to face an object.
[edit] showGizmos : bool
This flag will enable or disable gizmos drawn in the scene window of the editor. Gizmos are drawn when this is set to true, and the enemy is selected in the hierarchy. This flag is only available in the editor.
The gizmos for this script have the following meanings:
Green: This is the radius at which the enemy will start chasing the player (chaseStartDistance).
Red: This is the radius at which the enemy will stop chasing (chaseStopDistance).
Yellow: These are the waypoints on the path. The radius of these nodes is the waypointRadius.
Magenta: This is the current waypoint. The radius of this node is also the waypointRadius.
Blue: This shows the leashDistance for each node on the path.
[edit] Public Functions
These are used by the PathEnemyStates to implement the movement and behavior of the enemy.
[edit] bool CanMove
This checks if the enemy can currently move or not.
[edit] Transform GetCurrentWaypoint ()
This returns the Transform for the current waypoint along the path.
[edit] void UpdateCurrentWaypoint ()
This is checks the distance between the enemy and the next waypoint; if the enemy is close enough to the waypoint (within waypointRadius), the current waypoint is updated according to the settings for currentDirection, pathStep, and howToLoop.