Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Explain the Code | Improve the Flappy Bird Game
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

bookExplain the Code

Player Code

Initialization in Start Method

Purpose: Stores the player's initial position when the game starts.

Restarting Player Position

Purpose: Resets the player's position to the start position and stops any movement by setting the velocity to zero.

Handling Player Input in Update Method

Purpose: Captures the player's input (space key) to make the player "jump" by setting the y-velocity to 6, while constantly moving the player to the right by setting the x-velocity to 4.

Collision Handling

OnCollisionEnter2D

Purpose: Resets the player's position when colliding with objects tagged as "ground".

OnTriggerEnter2D

Purpose: Activates the win panel and plays a sound effect when the player collides with objects tagged as "win".

Summary

  • Movement: Player moves to the right continuously and jumps when the space key is pressed;
  • Restart: Player position resets upon collision with the ground;
  • Winning: Displays a win panel and plays a sound when reaching the win trigger.

SoundManager Class

Start Method

Purpose: Ensures there is only one instance of SoundManager in the game using the Singleton Pattern. Uses DontDestroyOnLoad(gameObject) to persist across different scenes.

PlayEffect Method

Purpose: Takes an integer index to identify which sound effect to play from the effects list. It checks if the index is within the valid range of the effects list and plays the specified sound effect using source.PlayOneShot(effects[index]).

Summary

  • Singleton Pattern: Ensures only one instance of SoundManager exists in the game, persisting across scenes;
  • Playing Sound Effects: Uses the PlayEffect method to play sound effects by specifying their index in the effects list;
  • Audio Source: Utilizes an AudioSource component to play the audio clips.

WinPanel Class

The WinPanel class provides functionality for the UI panel that appears when the player wins. It includes methods to return to the main menu or restart the level, each playing a sound effect and loading the appropriate scene.

Methods

ReturnToMainMenu Method

Purpose: Plays a sound effect (index 0) using SoundManager.instance.PlayEffect(0). Loads the main menu scene with build index 0 using SceneManager.LoadScene(0).

RestartLevel Method

Purpose: Plays a sound effect (index 0) using SoundManager.instance.PlayEffect(0). Loads the game scene named "Game" using SceneManager.LoadScene("Game").

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 1
We're sorry to hear that something went wrong. What happened?
some-alt