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

Create Main Menu

Main Menu

Script for Main Menu

The MainUI class is a simple script that provides functionality to start the game and quit the application. This script is intended to be used with UI buttons in Unity.

Class Definition and Methods

The StatTheGame method is designed to initiate the game by loading a new scene. Specifically, SceneManager.LoadScene(1) directs Unity to load the scene with the build index 1, which corresponds to its position in the Build Settings.

The Quit method allows for the application to be closed by calling Application.Quit(), although this functionality is only effective in a built application and not within the Unity Editor.

Usage in Unity

Attaching the Script

Create a GameObject (e.g., an empty GameObject or a UI element). Attach the MainUI script to the GameObject by dragging the script onto it or using the Add Component button in the Inspector.

Setting Up UI Buttons

Create a UI button (e.g., GameObject > UI > Button). Select the button and go to the Inspector. In the On Click () section, click the + button to add a new event. Drag the GameObject with the MainUI script attached into the object field. From the dropdown, select MainUI > functionName.

Game Manager

Script for Game Manager

The GameManager class is responsible for managing the state of the game. It uses a singleton pattern to ensure there is only one instance of GameManager in the game, manages the game state, and broadcasts state changes using events.

Class Definition and Member Variables

Methods

Awake Method

Purpose: Ensures there is only one instance of GameManager and prevents it from being destroyed when loading new scenes.

Singleton Pattern: Checks if an instance already exists. If so, it destroys the duplicate. Otherwise, it assigns the instance and marks the object to not be destroyed on loading a new scene.

Start Method

Purpose: Initializes the game state to Playing when the game starts and triggers the onGameStateChanges event.

FinishGame Method

Purpose: Sets the game state to Finished and triggers the onGameStateChanges event.

GameStates Enum

Purpose: Defines the possible states of the game.

Enemy React to Game State Changes

This method handles the game state changes. When the game state changes to Finished, the enemy's state is changed to idle.

Finish Panel

Script for Finish Panel

The FinishPanel class handles the display and functionality of the finish panel that appears when the game ends. This panel provides feedback to the player on whether they have won or lost and offers options to return to the main menu or quit the game.

Explanation

Methods

Start Method

Purpose: Subscribes to the GameManager.onGameStateChanges event to listen for changes in the game state.

GameManager_onGameStateChanges Method

Purpose: Handles the game state changes. When the game state is Finished, it activates the finish panel and calls OnGameFinished.

OnGameFinished Method

Purpose: Updates the finish panel's text based on whether the player is dead or alive.

Logic: If the player is dead, the method sets the text to "You lost" in red. If the player is alive, it sets the text to "You Won" in green.

BackToMainMenu Method

Purpose: Loads the main menu scene (build index 0) when called. This method is intended to be linked to a button in the UI.

Quit Method

Purpose: Quits the application when called. This method is intended to be linked to a button in the UI.

Summary

The FinishPanel class displays end-of-game results and offers options to return to the main menu or quit. It subscribes to GameManager.onGameStateChanges to update appropriately when the game finishes, enhancing the user experience with clear feedback and intuitive options.

1. What happens when the `FinishGame` method is called in the `GameManager` class?
2. What happens when the `GameManager` changes the game state to `GameStates.Finished`?

What happens when the FinishGame method is called in the GameManager class?

Select the correct answer

What happens when the GameManager changes the game state to GameStates.Finished?

Select the correct answer

Everything was clear?

Section 4. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt