Conteúdo do Curso
Fighting Game in Unity
Fighting Game in Unity
Add Particles
Explanation of the Changes On The Update Method
The Update
method in the enemy script now includes logic to update the state manager, ensure the enemy faces the player, and handle a specific case where the enemy is in a DeathState
. This logic was added to prevent the enemy from continuing to face the player after it has died.
Method Components
StateManager Update
stateManager.UpdateStates(Time.deltaTime)
This line updates the state manager, passing in the time elapsed since the last frame (Time.deltaTime
). It ensures that the current state is updated correctly every frame.
DeathState Check
if (stateManager.GetCurrentState() is DeathState) return;
This line checks if the current state is DeathState
.
If the enemy is in the DeathState
, the method returns early, preventing any further updates for facing the player or other behaviors.
Facing the Player
The following lines ensure the enemy always faces the player:
scale.x = transform.position.x > player.position.x ? -1 : 1;
This line checks if the enemy's x position is greater than the player's x position:
- If true,
scale.x
is set to-1
, making the enemy face left; - If false,
scale.x
is set to1
, making the enemy face right.
transform.localScale = scale;
This line applies the updated scale to the enemy's transform, ensuring it faces the correct direction.
Why We Did It Like This
Handling Death State
Adding the check for DeathState
ensures that the enemy does not continue to update its behavior or orientation after it has died. This is crucial for maintaining game logic and preventing unexpected behaviors after the enemy is supposed to be inactive.
Centralized Logic
By moving the player-facing logic into the Update
method, we centralize this behavior, reducing redundancy and improving code maintainability.
It ensures that the enemy always faces the player regardless of the current state, which simplifies state-specific code.
Explanation of Enemy Death Handling
The DeathState
class is a specific state that handles the enemy's behavior when it dies.
Why We Did It Like This
Handling Death Behavior
The DeathState
class specifically manages the behavior of the enemy when it dies, ensuring that the death animation is played and the state is marked as finished.
Animation Integration
Using animator parameters ensures that the death animation is correctly synchronized with the state change, providing a smooth and realistic experience.
Particle Spawn on Enemy Death
This code shows how the enemy handles its death when its health reaches zero. It transitions to the DeathState
and plays a death particle effect.
Code Breakdown
Health Check
if (health <= 0)
This condition checks if the enemy's health has dropped to zero or below.
Transition to DeathState
stateManager.ChangeState(deathState)
This line transitions the enemy to the DeathState
using the StateManager
.
Instantiate Death Particles
var deathParticle = Instantiate(deathParticles, transform.position, Quaternion.identity)
This line creates an instance of the death particle effect at the enemy's position.
deathParticles
is a prefab that contains the particle system for the death effect.
Quaternion.identity
ensures that the particles have no rotation.
Play Death Particles
deathParticle.Play()
This line plays the instantiated particle effect.
Destroy Death Particles
Destroy(deathParticle.gameObject, 5)
This line schedules the destruction of the particle effect GameObject
after 5 seconds, ensuring that it does not persist indefinitely in the game.
Why We Did It Like This
State Management:
Transitioning to DeathState
ensures that the enemy's death behavior is properly managed and that any specific logic for the death state is executed.
Visual Effects: Playing a death particle effect provides visual feedback to the player, enhancing the game experience.
Obrigado pelo seu feedback!