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

Contenido del Curso

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

bookRun State

Changes and Additions

isStateFinished Variable:

This is a boolean variable that indicates whether the state has finished its execution.

FinishState Method:

This is a new virtual method that sets isStateFinished to true. Being virtual means that derived classes can override this method if they need to perform additional actions when a state finishes.

Why These Changes Were Made

The changes introduce the isStateFinished variable to track state completion and the FinishState method to standardize state completion signaling, allowing derived classes to add custom logic upon state completion.

Run State

Let's look at the RunState class, which inherits from the State class. This class is responsible for handling the running behavior of our enemy. We'll focus on the additions and changes made to this class.

Constructor:

public RunState(Rigidbody2D rb, Animator animator, Transform p, float speed);

This constructor initializes the RunState with the necessary components: a Rigidbody2D for movement, an Animator for animations, a Transform to track the player, and a speed for how fast the enemy runs;

StartState Method:

public override void StartState(); This method sets isStateFinished to false and triggers the running animation by setting the "run" boolean parameter in the animator to true.

UpdateState Method:

public override void UpdateState(float DeltaTime); This method updates the enemy's position and orientation based on the player's position; It adjusts the enemy's scale to face the player, calculates the velocity based on the speed, and applies this velocity to the Rigidbody2D.

EndState Method:

public override void EndState() This method stops the running animation by setting the "run" boolean parameter in the animator to false.

How It Works for Our Enemy

  • Movement and Animation: By using Rigidbody2D and Animator, the RunState ensures the enemy can move smoothly and have corresponding animations. This makes the behavior visually and physically realistic;
  • Tracking Player Position: The player transform allows the enemy to always move towards the player, which is essential for chase or run behaviors;
  • Direction Handling: Adjusting the scale.x based on the player's position ensures the enemy faces the player correctly while running, adding to the realism;
  • Dynamic State Updates: The UpdateState method is called every frame to continuously adjust the enemy's movement and direction based on the player's position, making the enemy responsive and dynamic.

Enemy Initialization

Explanation

State Initialization:

The variables idle and runState are initialized with their respective states. The idle variable is an instance of IdleState with the animator passed in, while runState is an instance of RunState that includes a Rigidbody2D, Animator, Transform for the player, and a speed value.

StateManager Initialization:

stateManager = new StateManager(idle); The StateManager is initialized with the idle state as the starting state.

Transition Definitions:

  • FarAwayTransition: FarAwayTransition = new Transition(() => { return (ThresholdDistance < Vector2.Distance(transform.position, player.position)); }, new StatePourcentage(runState, 50f), new StatePourcentage(dashState, 50f));; This transition checks if the player is farther than ThresholdDistance;
  • toIdleTransition: toIdleTransition = new Transition(() => { return stateManager.GetCurrentState().isStateFinished; }, new StatePourcentage(idle, 100)); This transition checks if the current state is finished. If true, it transitions to idle state with 100% probability.
  • finishRunning: finishRunning = new Transition(() => { return (ThresholdDistance >= Vector2.Distance(transform.position, player.position)); }, new StatePourcentage(idle, 100)); This transition checks if the player is closer than ThresholdDistance. If true, it transitions to idle state with 100% probability.

Adding Transitions to StateManager:

These lines add the defined transitions to the stateManager.

Why We Did It Like This

Dynamic State Changes:

The transitions allow the enemy to dynamically change its behavior based on the player's position and the completion status of the current state, making the enemy more responsive and interactive within the game environment.

Conditional Transitions:

The use of conditions (like checking distance or state completion) ensures that state transitions occur logically and appropriately, enhancing gameplay realism.

Probability-Based Transitions:

Using StatePourcentage allows for probability-based transitions, adding an element of unpredictability and variety to the enemy's behavior.

What happens to the `animator` when the `StartState` method is called in the `RunState` class?

What happens to the animator when the StartState method is called in the RunState class?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 5
some-alt