Difference between revisions of "PathEnemyFSM"

From Headbone Creative
Jump to: navigation, search
(Updating API info)
(Updating API info)
 
Line 8: Line 8:
 
=== path: Transform[] ===
 
=== 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.
  
/// <summary>
+
The gizmos for this script have the following meanings:
/// Path enemy loop style.
+
/// </summary>
+
public enum PathEnemyLoopStyle {
+
None,
+
Loop,
+
PingPong,
+
};
+
+
/// <summary>
+
/// The setting for how to loop
+
/// </summary>
+
public PathEnemyLoopStyle howToLoop = PathEnemyLoopStyle.Loop;
+
+
/// <summary>
+
/// Path enemy path direction.
+
/// </summary>
+
public enum PathEnemyPathDirection {
+
None,
+
Forward,
+
Backward,
+
};
+
+
/// <summary>
+
/// The current direction.
+
/// </summary>
+
public PathEnemyPathDirection currentDirection = PathEnemyPathDirection.Forward;
+
  
/// <summary>
+
'''Green''': This is the radius at which the enemy will start chasing the player (chaseStartDistance).
/// The path.
+
 
/// </summary>
+
'''Red''': This is the radius at which the enemy will stop chasing (chaseStopDistance).
public ;
+
 
+
'''Yellow''': These are the waypoints on the path. The radius of these nodes is the waypointRadius.
/// <summary>
+
 
/// The chase start distance.
+
'''Magenta''': This is the current waypoint. The radius of this node is also the waypointRadius.  
/// </summary>
+
 
public float chaseStartDistance = 1.0f;
+
'''Blue''': This shows the leashDistance for each node on the path.
+
 
/// <summary>
+
== Public Functions ==
/// The chase stop distance.
+
These are used by the [[PathEnemyStates]] to implement the movement and behavior of the enemy.
/// </summary>
+
=== bool CanMove ===
public float chaseStopDistance = 2.0f;
+
This checks if the enemy can currently move or not.  
+
=== Transform GetCurrentWaypoint () ===
/// <summary>
+
This returns the [http://docs.unity3d.com/Documentation/ScriptReference/Transform.html Transform] for the current waypoint along the path.  
/// The chase speed.
+
=== void UpdateCurrentWaypoint () ===
/// </summary>
+
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.  
public float chaseSpeed = 0.5f;
+
+
/// <summary>
+
/// The rotate to path flag
+
/// </summary>
+
public bool rotateToPath = true;
+
+
/// <summary>
+
/// The rotate speed.
+
/// </summary>
+
public float rotateSpeed = 5.0f;
+
+
/// <summary>
+
/// The chase mode. (how to chase it)
+
/// </summary>
+
public PathEnemyChaseMode chaseMode = PathEnemyChaseMode.lerp;
+
+
/// <summary>
+
/// The waypoint radius.
+
/// </summary>
+
public float waypointRadius = 0.1f;
+
+
/// <summary>
+
/// The leash distance.
+
/// </summary>
+
public float leashDistance = 20.0f;
+
+
/// <summary>
+
/// The use leash.
+
/// </summary>
+
public bool useLeash = true;
+
+
/// <summary>
+
/// This flag indicates whether or not the scene is 2d or 3d.
+
/// This affects how the rotation is calculated.  
+
/// </summary>
+
public bool is2dScene = true;
+
  
  

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).

Contents

[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.

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox