Course Content
Fighting Game in Unity
Fighting Game in Unity
Send Projectile
The Projectile
class is responsible for managing the behavior of projectiles that the enemy can launch at the player. This class handles setting up the projectile's direction and speed, updating its movement, and handling collisions.
Setup Method
Purpose: This method sets up the initial state of the projectile.
Parameters: Takes a Vector2 direction
parameter to set the direction the projectile will travel.
Actions: Sets the direction of the projectile, gets the Rigidbody2D
component attached to the projectile, rotates the projectile to face the direction it will travel, and schedules the projectile to be destroyed after 6 seconds to prevent it from existing indefinitely.
Update Method
Purpose: This method updates the projectile's movement.
Actions: Sets the velocity of the Rigidbody2D
to move the projectile in the specified direction at the specified speed.
OnTriggerEnter2D Method
Purpose: This method handles collisions with other objects.
Parameters: Takes a Collider2D collision
parameter to detect collisions.
Actions: Logs the name of the object the projectile collided with, checks if the projectile collided with an object tagged "Player," and if the object has a component that implements the IGetAttacked
interface, it calls the GetAttacked
method on that component to deal damage. Finally, it destroys the projectile after it hits the player.
Why We Did It Like This
Modular and Reusable: The Projectile
class is designed to be modular and reusable for any type of projectile the enemy might use. By setting the direction and speed, various projectiles with different behaviors can be created.
Efficient Movement Handling: Using the Rigidbody2D
component for movement ensures that the projectile behaves according to Unity's physics system, providing smooth and realistic motion.
Collision Detection and Damage Handling: The OnTriggerEnter2D
method ensures that the projectile can detect collisions with the player and apply damage appropriately, making the interaction between the projectile and player effective.
SendProjectileState Class
The SendProjectileState
class is a specific state that handles the behavior of the enemy when it launches a projectile. This class inherits from the State
class and includes methods to start, end, and initiate the projectile attack action.
Constructor
The constructor initializes the SendProjectileState
with an Animator
for animations and an action to send the projectile. It sets these values to the corresponding member variables.
StartState Method
This method sets isStateFinished
to false
and triggers the projectile animation by setting the "projectil" boolean parameter in the animator to true
.
StartAction Method
This method calls sendProjectileFunc
, which executes the action defined for sending the projectile. This method is typically called by an animator event to synchronize the projectile launch with the animation.
EndState Method
This method stops the projectile animation by setting the "projectil" boolean parameter in the animator to false
.
Why We Did It Like This
Modular and Reusable:
The SendProjectileState
class is designed to be modular, allowing for flexible integration of different projectile sending behaviors through the use of System.Action
.
Animation Integration: By using animator parameters, the state ensures that the projectile launch is synchronized with the corresponding animation, providing a smooth and realistic experience.
Send Projectile Function
The SendProjectile
method is responsible for creating and launching a projectile towards the player. This method calculates the direction from the enemy to the player, instantiates the projectile, and sets it up to move in the calculated direction.
Method Definition and Components
Method Definition:
void SendProjectile()
: This method does not return a value and is designed to handle the projectile launch.
Components:
Vector2 direction = (player.position - transform.position).normalized;
This line calculates the direction from the enemy to the player by subtracting the enemy's position from the player's position. The .normalized
part ensures that the direction vector has a length of 1, making it a unit vector that only indicates direction.
Instantiate(projectile, projectileSendingPosition.position, Quaternion.identity).Setup(direction);
This line creates a new instance of the projectile at the specified position (projectileSendingPosition.position
) with no rotation (Quaternion.identity
). The Setup
method is called on the instantiated projectile to set its direction.
Linking With The Unity Animator
We are calling the StartAction
function using animator events. This allows us to specify the exact frame within the enemy's animation at which we want the projectile to be sent. Since the animation consists of many frames, it is important to pinpoint the precise moment for this action to occur.
Thanks for your feedback!