Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Rage State | Enemy Behavior
Fighting Game in Unity
course content

Course Content

Fighting Game in Unity

Fighting Game in Unity

1. Unity Animation System
2. Player Behavior
3. Enemy Behavior
4. Improve the Game
5. Improve the Flappy Bird Game

bookRage State

Explanation of the RageState Class

The RageState class is a specific state that handles the enemy's behavior when it enters a rage mode. This class inherits from the State class and includes methods to start, update, and end the rage state. It also includes actions to add health and change attack damage when the enemy is in rage mode.

Class Definition and Member Variables

Class Definition;

public class RageState : State This class inherits from the State class, meaning it must implement the abstract methods defined in State.

Member Variables:

Animator animator; Controls the animations.
System.Action addHealth; An action to add health to the enemy.
System.Action changingAttackDmg; An action to change the enemy's attack damage.

Constructor

The constructor initializes the RageState with the necessary components: an Animator for animations and two actions for adding health and changing attack damage. It sets these values to the corresponding member variables.

StartState Method

This method sets isStateFinished to false, triggers the rage animation by setting the "rage" boolean parameter in the animator to true, and calls changingAttackDmg to increase the enemy's damage.

UpdateState Method

This method is called every frame and calls addHealth to continuously add health to the enemy while in the rage state.

EndState Method

This method stops the rage animation by setting the "rage" boolean parameter in the animator to false.

Why We Did It Like This

Enhanced Enemy Behavior;

The RageState enhances the enemy's behavior by increasing its attack damage and continuously adding health, making it more challenging for the player; Using actions (System.Action) allows for flexible and modular modifications to the enemy's attributes.

Dynamic and Engaging Gameplay:

By having the enemy enter a rage state, the gameplay becomes more dynamic and engaging, as the player must adapt to the more powerful and resilient enemy.

How It Works in Context

Entering Rage State: When the enemy enters the RageState, the StartState method is called, triggering the rage animation and increasing the enemy's attack damage.

During Rage: The UpdateState method is called every frame. It continuously adds health to the enemy, making it more difficult for the player to defeat the enemy.

Exiting Rage State: When the enemy leaves the RageState, the EndState method is called, stopping the rage animation.

By structuring the RageState in this way, we ensure that the enemy can become significantly more powerful and resilient, providing a greater challenge for the player and enhancing the overall gameplay experience.

Initialize Rage State

Explanation

RageState Initialization:

rage = new RageState(animator, (...) => {...}, (...) => {...}); Creates a new RageState instance named rage;

Animator:

animator: Passes the animator to control animations;

Increase Health:

() => { health += 10f * Time.deltaTime; } A lambda function that increases the health by 10 units per second;

Change Attack Damage:

() => { attack1.ChangeAttackDmg(15); attack2.ChangeAttackDmg(25); } A lambda function that changes the attack damage of attack1 to 15 and attack2 to 25.

Rage Transition

The rageTransition is a transition that moves the enemy into a RageState when certain conditions are met. This transition is designed to make the enemy more formidable when its health drops below a certain threshold and it has not yet entered the rage state.

Transition Definition

Condition:

The transition checks if the enemy's health is less than or equal to 40 and if the enemy has not already entered the rage state (haveRaged is false). If both conditions are met, it sets haveRaged to true to ensure the enemy only enters the rage state once and returns true to trigger the transition.

State Pourcentage:

The transition uses new StatePourcentage(rage, 100f) to indicate that when the condition is met, the enemy will transition to the RageState with 100% probability.

Why We Did It Like This

Triggering Powerful Behavior: The rageTransition allows the enemy to become more powerful when its health is critically low, increasing its challenge to the player. This transition ensures the enemy only enters the rage state once, preventing repeated transitions into the rage state.

Dynamic and Challenging Gameplay: By introducing the rage state under critical health conditions, the gameplay becomes more dynamic and challenging, requiring the player to adapt their strategy.

How It Works in Context

Health Check: During the game, as the enemy takes damage, its health is monitored. When the enemy's health drops to 40 or below and it has not raged yet, the rageTransition condition is met.

Triggering Rage State: Once the condition is met, haveRaged is set to true, and the transition to the RageState is triggered with 100% probability.

Entering Rage State: The RageState is initiated, increasing the enemy's attack damage and continuously adding health, making the enemy significantly more difficult to defeat.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 8
some-alt