Course Content
Fighting Game in Unity
Fighting Game in Unity
Attack State
Explanation of the AttackState Class
The AttackState
class is a specific state that handles the attacking behavior of the enemy. This class inherits from the State
class and includes methods to start, update, and end the attack state, as well as handle the attacking action.
Class Definition and Member Variables
Class Definition:
public class AttackState : State
;
This class inherits from the State
class, meaning it must implement the abstract methods defined in State
.
Member Variables:
string attackStateName;
The name of the attack state used in the animator.
bool isAttacking;
A flag to indicate if the enemy is currently attacking.
Animator animator;
Controls the animations.
Transform transform;
The transform of the enemy, used for positioning and raycasting.
float range;
The attack range.
int damage;
The damage dealt by the attack.
LayerMask playerLayer;
The layer mask used to identify the player.
Constructor
The constructor initializes the AttackState
with the necessary components: a Transform
for positioning, an Animator
for animations, a layer mask to identify the player, the attack state name, the damage value, and the attack range. It sets these values to the corresponding member variables.
StartState Method
This method sets isStateFinished
to false
, triggers the attack animation by setting the appropriate boolean parameter in the animator to true
, and resets the isAttacking
flag to false
.
EndState Method
This method stops the attack animation by setting the appropriate boolean parameter in the animator to false
.
UpdateState Method
This method is called every frame and triggers the Attack
method to perform the attack action.
StartAction Method
This method sets isAttacking
to true
, indicating that the attack action has started.
FinishMovement Method
This method sets isAttacking
to false
, indicating that the attack action has ended.
Attack Method
This method performs the attack action. If isAttacking
is true
, it casts a ray in the direction the enemy is facing. If the ray hits an object on the playerLayer
, it calls the GetAttacked
method on the hit object, dealing damage and ending the attack action.
Why We Did It Like This
-
Control Over Attacking Behavior: The
isAttacking
flag allows for precise control over when the attack should occur, making the behavior more responsive to game events; -
Dynamic Attack Execution: By continuously updating the enemy's attack state, the
AttackState
ensures the enemy can accurately and dynamically attack the player; -
Animation Integration: Using animator parameters ensures that the enemy's animations are correctly synchronized with its attacks, providing a smooth and realistic experience.
How It Works in Context
Entering Attack State: When the enemy enters the AttackState
, the StartState
method is called, triggering the attack animation and resetting the isAttacking
flag.
During Attack: The UpdateState
method is called every frame. It triggers the Attack
method to check if an attack should occur.
Starting Attack: The StartAction
method sets isAttacking
to true
, allowing the Attack
method to perform the attack action.
Performing Attack: The Attack
method casts a ray to detect the player and deals damage if the player is hit.
Finishing Attack: The FinishMovement
method sets isAttacking
to false
, stopping the attack action.
Exiting Attack State: When the enemy leaves the AttackState
, the EndState
method is called, stopping the attack animation.
By structuring the AttackState
in this way, we ensure that the enemy can perform responsive and dynamic attacks on the player, with smooth transitions between animations and states.
Close Transition:
closeTransition:
This transition checks if the player is within a certain horizontal distance (ThresholdDistance
) from the enemy.
If the condition is met, it randomly transitions to either attack1
or attack2
with equal probability (50% each);
Code Example:
Adding closeTransition to Idle State:
stateManager.AddStateTransition(idle, closeTransition);
This line adds the closeTransition
to the idle
state, allowing the state machine to check for this condition when the enemy is in the idle state.
Why We Did It Like This
The transition system enables the enemy to switch to an attack state based on the player's proximity, enhancing engagement. The probabilistic approach for choosing between different attacks adds unpredictability, increasing the game's challenge and interest.
How It Works in Context
Idle State:
While the enemy is in the idle
state, it constantly checks if the player is within the ThresholdDistance
using the closeTransition
.
If the player is close enough, the closeTransition
triggers, and the enemy transitions to either attack1
or attack2
.
Attack States:
Once in attack1
or attack2
, the enemy performs the attack.
After the attack finishes, the existing toIdleTransition
transitions the enemy back to the idle
state, ready to check for the closeTransition
condition again.
By adding this closeTransition
, we ensure that the enemy can engage the player more dynamically and unpredictably when the player is within a close range. This enhances the gameplay by making the enemy's behavior more responsive and engaging.
Thanks for your feedback!