Unity Basics Course: Creating an Ending Screen and Exiting the Room

Hello! In this tutorial, we will explore how to create an ending screen after making a game in Unity and implement the exit functionality. This tutorial is aimed at those with a basic understanding of Unity and will proceed step by step. Each step will include necessary code examples and explanations.

1. Course Overview

This course consists of two main parts:

  • Creating an ending screen
  • Implementing the exit functionality

Each part will show how to utilize Unity’s user interface (UI) to enhance the game’s experience. Through this tutorial, you will learn to add an ending screen to your game and provide a way for users to easily and conveniently exit the game.

2. The Need for an Ending Screen

Providing an appropriate ending screen to players after the game ends is an important part of the gaming experience. Through the ending screen, players can review their achievements in the game and choose an option to restart. A good ending screen helps leave a positive impression on players even after they have finished the game.

3. Creating the Ending Screen

3.1 Setting Up the Project

First, open Unity and create a new 2D project. Let’s name the project ‘EndingScreenExample’.

3.2 Setting Up the UI

To create the ending screen, we need to set up the UI. Follow these steps:

  1. Select GameObject > UI > Canvas in the Unity editor to add a canvas.
  2. Add UI > Panel inside the canvas to create a background panel.
  3. Adjust the size of the panel to cover the entire screen.
  4. Add UI > Text inside the panel to enter the ending message. For example: “You have completed the game!”
  5. Add UI > Button to create a ‘Return to Main Menu’ button.
  6. Change the button text to ‘Restart’ or ‘Main Menu’ in the properties.
  7. Additionally, add background music or sound effects to make the ending screen more interesting.

3.3 Writing the Script

Now, let’s write a C# script to implement the functionality of the ending screen. Create a script like the following:

using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;

    public class EndingScreen : MonoBehaviour
    {
        public GameObject endingPanel;

        public void ShowEndingScreen()
        {
            endingPanel.SetActive(true);
        }

        public void RestartGame()
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }

        public void QuitGame()
        {
            Application.Quit();
        }
    }
    

In the above script, the ShowEndingScreen method shows the ending screen, and the RestartGame and QuitGame methods handle restarting or quitting the game when each button is clicked.

3.4 Connecting Functions to Buttons

Connect the script to the buttons so that the functions are triggered when they are clicked. After selecting the button, add the On Click() event in the inspector and connect it to the RestartGame and QuitGame methods of the EndingScreen script.

4. Implementing the Exit Functionality

Now let’s add a feature that allows users to easily exit the room when they want to quit the game. This function primarily operates through button clicks and can be implemented through the steps below.

4.1 Adding a UI Button

Add a UI > Button to the main menu or ending screen of the game. Set the button text to ‘Exit Room’.

4.2 Writing the Exit Script

Write a new script to implement the exit functionality. Refer to the example below:

using UnityEngine;

    public class ExitGame : MonoBehaviour
    {
        public void ExitRoom()
        {
            Application.Quit();
            #if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
            #endif
        }
    }
    

The above code provides the functionality to exit the game when leaving the room and also handles testing in editor mode.

4.3 Connecting the Exit Functionality to the Button

Connect the ExitGame script to the button to activate the functionality of exiting when clicked. Select the button and set the On Click() event in the inspector to connect with the ExitRoom method of the ExitGame script.

5. Comprehensive Testing

Once all setups are complete, run the game to test if the functionality works correctly. Check if the ending screen appears and if each function operates upon button clicks. If everything works well, you have successfully implemented the ending screen and exit functionality in Unity!

6. Conclusion and Tips

Through this tutorial, you have learned how to implement a simple ending screen and exit functionality in Unity. Consider the following:

  • Enhance UI design by adding animation effects.
  • You can display various ending messages based on the game result.
  • Consider adding features to record and display the player’s score or achievements.
Note: Feel free to test and modify to create your own unique ending screen!

This concludes the Unity basics tutorial on creating an ending screen and exit functionality. I hope it helps you in your game development journey!

Unity Basics Course: Package Insertion (Import)

Unity is one of the most popular game development platforms in the world, used for creating 2D and 3D games, simulations, augmented reality (AR), and virtual reality (VR) content. In this course, we will learn in detail about the basic concepts of Unity and how to import packages.

1. Introduction to Unity

Unity is a powerful engine that helps create games and applications for various platforms. It is suitable for both beginners and professionals, with a wide range of features and strong community support. With Unity, you can create visually appealing content with minimal coding.

2. Basic Elements of Unity

Unity is composed of several basic elements. Let’s explain them in an easy-to-understand way:

  • Scene: A space that contains all game objects.
  • Game Object: Everything in Unity, including models, buildings, cameras, etc.
  • Component: An element that gives functionality to game objects. For example, components like physics properties and scripts.
  • Package: A compressed file containing various features and assets. Packages can be purchased or downloaded for free from the Unity Asset Store.

3. What is Package Import?

Package import refers to the process of bringing a package downloaded from an external source into a Unity project. This allows developers to quickly utilize various assets and functionalities to efficiently build their projects. Through package import, you can add 3D models, textures, scripts, and other assets to your project.

3.1 Types of Packages to Import

There are several types of packages, each designed for specific purposes. Commonly used packages include:

  • Asset Packages: Composed of various assets, such as models, sprites, animations, and audio files.
  • Tool Packages: Help simplify tasks by adding specific features or tools. Examples include physics engines and UI toolkits.
  • SDK (Software Development Kit): A collection of development tools for integrating with specific platforms or services. Used for integrating with VR devices, AR services, etc.

4. How to Import Packages

Now, let’s take a closer look at how to import packages in Unity. This process can be easily performed in the Unity Editor.

4.1 Downloading from the Unity Asset Store

The Unity Asset Store is the official platform providing various scripts, 3D models, and assets. You can follow the steps below to download and import a package:

  1. Launch the Unity Editor and select Asset Store from the Window menu.

  2. When the Asset Store opens, search for the desired package.

  3. Click the buy or download button to download the asset.

4.2 Importing Downloaded Packages

After downloading a package, here’s how to import it into your project:

  1. Click the Assets menu in the Unity Editor.

  2. Select Import Package and then click Custom Package.

  3. When the file explorer opens, select the downloaded package file (.unitypackage) and click the Open button.

  4. A list of assets to import will be displayed. Select the necessary assets and click the Import button.

4.3 Directly Importing from the Unity Asset Store

There is also a method to directly import packages from the Unity Asset Store. This method is particularly advantageous for quickly importing frequently used packages.

  1. Click the Window menu in the Unity Editor and select Asset Store.

  2. Search for and select the desired package in the asset store.

  3. On the package page, click the Import button to import it directly into the project.

5. Precautions When Importing Packages

There are some precautions to take during the package import process:

  • Version Compatibility: Check if the package is compatible with the current version of Unity you are using. If not compatible, functionalities may not work properly.
  • File Size: Importing large packages can slow down your project. It is advisable to selectively import only the necessary assets.
  • Dependencies: Some packages may have dependencies on specific components or libraries. Ensure these dependencies are satisfied.

6. Package Management

After importing packages, it is important to manage them properly in your project. Here are the methods for package management:

6.1 Cleaning Up Unused Packages

Unneeded packages can increase the size of the project and degrade performance. It is advisable to clean up such packages.

  1. Find the used package in the project view, right-click, and select Delete to remove it.

6.2 Updating Packages

If a newer version of a package is released, you can update it to access new features. The update process is as follows:

  1. Open the Unity Asset Store and navigate to My Assets.

  2. Check for any packages that can be updated and click the Update button.

7. Conclusion

Importing packages in Unity is an important process for quickly adding the necessary assets and functionalities to your project. Through this process, developers can understand the basic elements and work efficiently. We hope this guide on package import helps you create a more productive development environment in Unity.

If you found this course helpful, we look forward to your feedback. If you have any questions or need additional information, please leave a comment. In the next course, we will learn about the basic concepts of scripting in Unity.

Unity Basics Course: Following the Player Character

Hello! In this tutorial, we will learn in detail how to create an enemy character that chases the player character using Unity. Understanding character movement and AI (Moving AI), which are fundamental elements of game development, will be very helpful. This tutorial will be explained step by step so that beginners who are not familiar with Unity can follow along.

Table of Contents

1. Installing Unity and Setting Up the Project

First, let’s install Unity and create a new 3D project. Please download and install Unity Hub from the official Unity website, then follow these steps.

  1. Run Unity Hub and click the New Project button.
  2. Enter a name for the project, select the 3D template, and then click the Create button.
  3. Once the project is created, the basic scene environment will appear.

2. Creating the Player Character

To create the player character, we will use basic 3D objects. We will proceed with the following steps.

  1. Right-click in the Hierarchy window and select 3D Object > Capsule. We will use this as the player character.
  2. Adjust the Transform component of the Capsule to set an appropriate size.
  3. In the Inspector window, click the Add Component button and add a Rigidbody to apply gravity.
  4. Also, set the character to touch the ground using a Collider.

3. Creating the Enemy Character

Now let’s add the enemy character. The enemy character can also be created using a 3D object.

  1. Right-click in the Hierarchy window again and select 3D Object > Cube to create the enemy character.
  2. Adjust the Transform component of the Cube to set the size and position of the enemy character.
  3. Add a Rigidbody component to the enemy to enable physical interactions.

4. Writing the Tracking Script for the Enemy Character

Now let’s write a script to allow the enemy character to track the player. Here’s an example of a C# script.

using UnityEngine;

public class EnemyController : MonoBehaviour
{
    public Transform player; // Player's Transform
    public float speed = 2.0f; // Speed of the enemy character
    public float detectionRange = 5.0f; // Range within which the enemy can chase

    void Update()
    {
        // Calculate distance to the player
        float distance = Vector3.Distance(transform.position, player.position);
        
        // Move towards the player if within range
        if (distance < detectionRange)
        {
            Vector3 direction = (player.position - transform.position).normalized;
            transform.position += direction * speed * Time.deltaTime;
        }
    }
}

Using the code above, set up the enemy character to chase the player. Attach the script to the enemy character object and drag the player character into the player field to add it.

5. Testing and Optimization

Now that everything is set up, run the game in play mode to check if the enemy character chases the player properly.

You should also consider performance optimization. As the number of enemies increases, avoid simply having all enemies chase the player; consider additional logic to limit the number of enemies that chase.

6. Conclusion

In this tutorial, we learned how to implement an enemy character that chases the player character using Unity. In this process, we created basic 3D objects and wrote scripts to implement game AI. These fundamental elements will serve as a basis for creating more complex game logic.

Continue to learn more features through various Unity tutorials in the future. Thank you!

Unity Basic Course: Conditional Statements – else if

Unity is a widely used engine for game development, supporting many features for creating interactive content. Since games provide various outcomes based on player actions, conditional statements play a very important role in game logic. In this tutorial, we will take a closer look at one of the conditional statements, ‘else if’.

1. Basic Concept of Conditional Statements

A conditional statement allows a program to perform different actions based on certain conditions. The ‘if’ statement evaluates a condition and executes specific code when the condition is true. The ‘else’ statement defines the code that will be executed if the condition is false. The ‘else if’ statement provides a way to evaluate multiple conditions sequentially.

2. Basic Syntax

The basic syntax for using a conditional statement is as follows:

if (condition) {
    // Code to execute if the condition is true
} else if (condition2) {
    // Code to execute if condition2 is true
} else {
    // Code to execute if all previous conditions are false
}
    

Here, ‘condition’ is a variable or calculated expression that determines whether it is true (when it has a true value) or false (when it has a false value).

3. Example of Using ‘else if’

Let’s look at an example of handling more complex conditions using ‘else if’. The example below implements an NPC that reacts based on the player’s health.

Example Code

using UnityEngine;

public class PlayerHealth : MonoBehaviour {
    public int playerHealth = 100;

    void Update() {
        if (playerHealth > 75) {
            Debug.Log("The player is healthy.");
        } else if (playerHealth > 50) {
            Debug.Log("The player is slightly injured.");
        } else if (playerHealth > 25) {
            Debug.Log("The player is seriously injured.");
        } else {
            Debug.Log("The player is in critical condition!");
        }
    }
}
    

The above code outputs an appropriate message based on the player’s health every frame. It allows handling multiple states clearly by separating conditions.

4. Applications of Conditional Statements

Conditional statements are useful in various cases:

  • Game State Management: Different logic can be executed based on the game’s progress. For instance, checking whether the player has won or lost.
  • AI Behavior Control: NPCs can be programmed to respond differently based on the player’s actions.
  • UI Response: UI elements can be changed based on user input.

5. Comparison of ‘else if’ and switch Statements

Another method to evaluate conditions besides the ‘else if’ statement is the ‘switch’ statement. The switch statement allows handling multiple conditions based on the value of a specific variable. Let’s consider a simple example.

int score = 85;

switch (score) {
    case 90:
        Debug.Log("A");
        break;
    case 80:
        Debug.Log("B");
        break;
    case 70:
        Debug.Log("C");
        break;
    default:
        Debug.Log("Not applicable");
        break;
}
    

In this example, different messages are output based on the value of the ‘score’ variable. Unlike the ‘else if’ statement, the switch statement can express handling specific values more concisely.

6. Performance of Conditional Statements

Having many conditional statements can impact performance. Particularly when using ‘else if’, the order of conditions is important. It is advisable to place the conditions that are most frequently executed at the top. Reducing unnecessary condition evaluations can help optimize performance.

7. Conclusion

Through this tutorial, we have increased our understanding of conditional statements in Unity, especially the ‘else if’ statement. In game development, conditional statements are essential for reflecting player actions and handling various game situations. By setting various conditions and implementing appropriate logic, you can create more dynamic games.

8. References

For those interested in learning more in-depth content, please refer to the materials below:

Conditional statements are essential knowledge for working with Unity. The deeper your understanding, the more the nature and appeal of the games you develop will improve. We hope you will embrace user feedback, modify your code, and build your learning experience.

Basic Unity Course: Implementing Network Room Creation

In this course, we will learn how to implement the feature of creating a simple network room using the Unity engine. Unity is a widely used platform for game development, and the latest networking framework has made multiplayer game development easier than ever. This tutorial will cover the basics of using Unity and its networking features while explaining how to create and manage a simple multiplayer room.

1. Project Setup

Before starting with Unity, you need to set up a project. Follow the steps below:

  1. Run Unity Hub and create a new project.
  2. Select ‘3D’ as the template and appropriately name your project.
  3. Click the ‘Create’ button to create the project.

After creating the project, set up a basic scene through the Unity editor. Right-click in the Hierarchy window to add a new GameObject and select Terrain to create the basic terrain.

2. Installing the Networking Package

The basic networking functionality of Unity can be installed through the Unity Package Manager. Follow the steps below to install the package:

  1. Select Window > Package Manager.
  2. Click the plus (+) button and select ‘Add package from Git URL’.
  3. Enter the following URL to add the ‘Mirror’ package:
  4. https://github.com/vis2k/Mirror.git

Mirror is an open-source networking library for use with Unity. This package allows you to implement multiplayer games.

3. Setting up the Network Manager

To create a network room, you need to set up the NetworkManager. The NetworkManager is a crucial component that manages all network activities. Follow the steps below to set it up:

  1. Create an empty GameObject in the Hierarchy and rename it to ‘NetworkManager’.
  2. Add the ‘NetworkManager’ component to this GameObject.
  3. Also, add the ‘NetworkManagerHUD’ component to create a basic UI.

Now you are ready to set up the network room. NetworkManagerHUD provides a basic UI to create a host or connect a client.

4. Creating and Connecting to a Room

We will add functionalities to create and connect to a room within the game. You will need to write scripts for this. Follow the steps below:

4.1 Creating a Room Creation Script

  1. Create a Scripts folder in the Project window and create a ‘RoomManager.cs’ file inside it.
  2. Implement the room creation functionality by entering the following code:
  3. using UnityEngine;
    using Mirror;
    
    public class RoomManager : NetworkManager
    {
        public override void OnStartServer()
        {
            Debug.Log("Server starting");
        }
        
        public override void OnStartClient()
        {
            Debug.Log("Client connected");
        }
    
        public void CreateRoom()
        {
            NetworkServer.Listen(7777);
            Debug.Log("Room created");
        }
    
        public void JoinRoom()
        {
            NetworkClient.Connect("127.0.0.1", 7777);
            Debug.Log("Connecting to room...");
        }
    }

4.2 Connecting the UI and Script

  1. Connect the ‘CreateRoom’ method to be called when the ‘OnHostButton’ of NetworkManagerHUD is clicked.
  2. Set it up so that the ‘JoinRoom’ method is called when the ‘OnClientButton’ is clicked.

5. Setting up Player Prefab

When a player enters a room, you need to set what object will be instantiated. Here’s how to set up the player prefab:

  1. Create a new GameObject and name it ‘Player’.
  2. Add a ‘NetworkIdentity’ component to this GameObject and check ‘Local Player Authority’.
  3. Make this GameObject a Prefab and save it in the Resources folder.
  4. Add the following code to the RoomManager script to set the player prefab:
  5. public GameObject playerPrefab;
    
        public override GameObject OnStartPlayer(NetworkConnection conn)
        {
            return Instantiate(playerPrefab);
        }

6. Testing and Debugging

Now that all the settings are complete, it’s time to run a test. Check if the network room creation and client connection work properly:

  1. Click the ‘Play’ button in the Unity editor to start the server.
  2. Run a new instance of Unity editor to connect the client.
  3. Check the console logs of both server and client to verify the connection status.

7. Conclusion

You have now implemented the basic functionalities of creating and connecting to a simple network room. Through this, you have gained an understanding of the fundamentals of multiplayer game development, and learned the necessary steps to add more complex networking features in the future.

Based on what you learned from this course, you can expand on it by implementing features such as:

  • Implementing a room listing feature
  • Adding voice or text chat functionality
  • Implementing custom player character spawning features

Network development in Unity has endless possibilities. Continue to explore the joy of multiplayer game development through various practice and experiments.