Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Dash 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

bookDash State

Animator Controller

Purpose of AnimatorController

The AnimatorController class is designed to manage and trigger behaviors of the enemy through animator events. This allows for seamless integration of animation transitions and state changes, ensuring that the enemy's behavior matches its animations.

Class Definition and Member Variable

Class Definition: public class AnimatorController : MonoBehaviour defines a class that inherits from MonoBehaviour, allowing it to be attached to a GameObject in Unity.

Member Variable: [SerializeField] Enemy enemy; is a serialized field that allows you to link an Enemy object to the AnimatorController directly from the Unity Inspector. The enemy variable is a reference to the enemy script, which contains functions that control the enemy's behavior.

Methods

FinishMovement Method: public void FinishMovement(): This method calls enemy.FinishMovement(). It is designed to be called by an animator event, signaling the end of a movement animation.

EndCurrentState Method: public void EndCurrentState(): This method calls enemy.EndCurrentState(). It is intended to be called by an animator event to signal the end of the current state.

Integration with Animator Events

Animator Events: Animator events are special events that can be placed on animation timelines within Unity's Animator. These events can call functions in scripts attached to the same GameObject or its children.

How It Works: During an animation, such as a running or attacking animation, specific points in the timeline can trigger events.

Dash State

Explanation of the DashState Class

The DashState class is a specific state that handles the dashing behavior of the enemy. This class inherits from the State class and includes methods to start, update, and end the dash state, as well as handle the finish movement event.

Constructor

The constructor initializes the DashState with the necessary components: a Rigidbody2D for movement, an Animator, a Transform for the player, and a dash speed. It sets the initial scale and initializes the velocity vector.

StartState Method

This method sets isStateFinished to false and triggers the dashing animation by setting the "dash" boolean parameter in the animator to true. It also resets the stopDashing flag to false.

FinishMovement Method

This method sets stopDashing to true, which will stop the dashing action in the UpdateState method.

UpdateState Method

This method updates the enemy's position and orientation based on the player's position. If stopDashing is true, the method returns early, stopping further updates. 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

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

Why We Did It Like This

The stopDashing flag allows for precise control over when the dash should stop, making the behavior more responsive to game events. By continuously updating the enemy's position and direction, the DashState ensures the enemy can quickly and accurately dash towards the player. Using animator parameters ensures that the enemy's animations are correctly synchronized with its movements, providing a smooth and realistic experience.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 6
some-alt