Unity Basics Course: Detecting Input Signals from Keyboard/Mouse

Unity is a powerful engine for game development that supports game development across various platforms.
The input handling system of a game is essential for interacting with players, and it is very important to learn how to recognize the player’s commands through keyboard and mouse input.
This course will provide a detailed explanation of how to detect input signals from the keyboard and mouse in Unity.
Through this, you will learn how to handle basic user input and implement various features of the game based on this knowledge.

1. Overview of Unity Input System

Unity provides a basic input system that helps developers easily manage input.
The input system collects input from various devices such as keyboards, mice, and gamepads and converts it into commands.

The flow of input in Unity is as follows:

  • Input Event Detection: The user sends commands using input devices.
  • Input Processing: These input events are detected by Unity’s input processing system.
  • Interacting with Game Objects: The input events allow interaction with objects in the game.

2. Detecting Keyboard Input

In Unity, the Input class is used to detect keyboard input. This class provides various methods to check if a specific key is pressed or being held down.
The main methods are:

  • Input.GetKey(KeyCode): Detects if a specific key is being held down.
  • Input.GetKeyDown(KeyCode): Detects the moment a specific key is pressed for the first time.
  • Input.GetKeyUp(KeyCode): Detects the moment a specific key is released after being pressed.

2.1 Keyboard Input Example

Below is a simple script example that detects keyboard input. This script outputs a message to the console when a specific key is pressed.

using UnityEngine;

public class KeyboardInputExample : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            Debug.Log("W key has been pressed.");
        }
        if (Input.GetKeyUp(KeyCode.W))
        {
            Debug.Log("Released the W key.");
        }
        if (Input.GetKey(KeyCode.S))
        {
            Debug.Log("S key is being held down.");
        }
    }
}

2.2 Key Input Response

Let’s add an example where the player object moves in response to key input.
The code below moves the player character using the W, A, S, D keys.

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed = 5f;

    void Update()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        transform.Translate(movement * moveSpeed * Time.deltaTime);
    }
}

3. Detecting Mouse Input

Mouse input can be used in various ways in Unity, including click detection, mouse movement, and scrolling.
The most basic way to detect mouse input is by using Input.mousePosition and Input.GetMouseButton methods.

3.1 Detecting Mouse Clicks

To detect mouse clicks, you can use Input.GetMouseButton(int button). The button parameter accepts 0 (left button), 1 (middle button), and 2 (right button).

using UnityEngine;

public class MouseClickExample : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("Left mouse button has been clicked.");
        }
    }
}

3.2 Mouse Position and Dragging

Let’s create an example that tracks the mouse position and adds functionality to drag the mouse to move an object.

using UnityEngine;

public class MouseDragExample : MonoBehaviour
{
    private Vector3 offset;
    private Camera mainCamera;

    void Start()
    {
        mainCamera = Camera.main;
    }

    void OnMouseDown()
    {
        offset = transform.position - GetMouseWorldPosition();
    }

    void OnMouseDrag()
    {
        transform.position = GetMouseWorldPosition() + offset;
    }

    private Vector3 GetMouseWorldPosition()
    {
        Vector3 mouseScreenPosition = Input.mousePosition;
        mouseScreenPosition.z = mainCamera.nearClipPlane; // Camera's near clipping plane
        return mainCamera.ScreenToWorldPoint(mouseScreenPosition);
    }
}

4. Integrating Input System into Your Game

The examples so far have shown how to detect and respond to inputs individually. In practice, games need to integrate various inputs to create more complex responses.
By integrating the input system, you can make player actions more natural and intuitive.

4.1 Interacting with User Interface (UI)

Interaction with the UI is an important part of the input system. For example, you can perform specific actions when a button is clicked.
Let’s look at how to create a button using Unity’s UI system and interact with that button.

using UnityEngine;
using UnityEngine.UI;

public class ButtonClickExample : MonoBehaviour
{
    public Button myButton;

    void Start()
    {
        myButton.onClick.AddListener(OnButtonClick);
    }

    void OnButtonClick()
    {
        Debug.Log("Button has been clicked.");
    }
}

4.2 Complex Input Processing

Learn how to handle keyboard, mouse, and UI inputs together in a game.
A simple example is to move a game object by clicking on it while simultaneously using the keyboard to perform other actions.

using UnityEngine;

public class CombinedInputExample : MonoBehaviour
{
    public float moveSpeed = 5f;

    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                transform.position = Vector3.MoveTowards(transform.position, hit.point, moveSpeed * Time.deltaTime);
            }
        }

        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
        }
    }
}

5. Conclusion

In this course, we explored how to detect keyboard and mouse input in Unity.
We learned the basic methods for handling user input and integrating this into various features of the game.
Now you are ready to explore creative ideas based on these input systems.
As the next step, it is recommended that you study more complex input processing and how to optimize user experience.

By gaining more information and hands-on experience, I hope you will develop a deeper understanding of Unity’s input system.
Now, start your game development journey!

Unity Basics Course: Extracting and Building Results

The first stage of game development is to create a prototype from an idea and then develop it into a completed project. Unity is a powerful engine that makes this process very straightforward. In this tutorial, we will explain in detail how to extract and build the results from a Unity project.

1. What is Unity?

Unity is a multi-platform game engine chosen by countless game developers. It provides the capability to create games for various platforms such as PC, mobile, and consoles, and supports developers’ productivity with powerful visual scripting and a rich Asset Store.

2. Setting Up a Unity Project

To get started with Unity, you first need to create a new project. Here are the steps to set up a project:

2.1 Creating a Project

After launching Unity Hub, click the ‘New Project’ button. Choose a template for the project, determine the project’s name and location, and then click ‘Create’.

2.2 Configuring the Environment

Once the project is created, adjust the game’s resolution and other basic settings through the Unity Editor’s settings. You can select the target platform from the File > Build Settings menu.

3. Understanding the Basic Elements of Unity

Before starting work in Unity, you need to understand the basic elements. The main components are as follows:

  • Scenes: Spaces that make up specific levels or environments of the game.
  • Game Objects: Refers to all objects in Unity, which gain functionality through various components.
  • Components: Properties that add various functionalities to each game object. Includes transform, renderer, collider, etc.

4. Extracting the Results

Once the game project is completed, you need to extract the results. The process of extracting results is as follows:

4.1 Opening Build Settings

Click File > Build Settings in the top bar of Unity to open the build settings window.

4.2 Selecting a Platform

Select the target platform. The way the game operates varies depending on the platform, so it needs to be selected carefully. For example, you can choose from various platforms such as PC, mobile, webGL, etc.

4.3 Adding Scenes

Select the scenes to be included in the game and click the Add Open Scenes button to add the scenes to the build.

5. Building

The building process is as follows:

5.1 Setting Up Build Files

Select the storage location for the build files and finally click the Build button. During this process, the build will start, and it may take some time.

5.2 Running After Build Completion

Once the build is complete, an executable file will be created in the selected location. Run the file to test the game and finally check the results.

6. In Case of Issues

Various errors may occur during the build process. Common issues and their solutions are as follows:

  • Reference Errors: Occurs if the script is not properly connected. Check the error messages in the Unity console and correct the issue.
  • Build Failure: Occurs when necessary files are missing or settings are incorrect. Recheck the build settings and ensure all necessary files are included.

7. Conclusion

In this tutorial, we explored the basic concepts of Unity along with the process of building a game. Unity is a platform with immense potential, used by many game developers. Based on what you learned in this tutorial, try tackling more complex and diverse game development!

8. Additional Resources

For deeper learning, please refer to the following resources:

Unity Basics Course: Player Synchronization and Character Attacks

As the importance of multiplayer games in modern game development increases, synchronization between players and an attack system are essential. This tutorial will provide a detailed explanation of how to implement synchronization and attacks between player characters in a multiplayer game using Unity.

1. Overview of Unity and Multiplayer Game Development

Unity is a powerful game engine that supports cross-platform development, allowing you to create both 2D and 3D games. Unity’s multiplayer capabilities offer various options such as Photon and Unity Multiplayer. In this tutorial, we will use Photon Unity Networking (PUN) to implement the synchronization and attack systems.

2. Setting up Photon Unity Networking (PUN)

To use PUN, you first need to download and install the Photon PUN 2 package from the Unity Asset Store. Alternatively, you can download the PUN SDK from the official Photon website.

2.1 Installing PUN

  1. Open the Unity editor and create a new project.
  2. Access the Asset Store, search for ‘Photon PUN 2’, and download it.
  3. Import the PUN package into your project.
  4. Run the Photon setup wizard to enter your app ID and complete the setup.

2.2 Setting up the Basic Scene

After installing Photon, set up the basic scene. You can manage the synchronization of each player by creating instances and adding the PhotonView component.

3. Setting up Player Characters and Animations

To set up the player character, import a model and add animations. We will use Rigidbody and Capsule Collider.

3.1 Importing Character Model

First, import your character model into the Unity project. After importing, it needs to be converted into a prefab.

3.2 Creating Player Prefab

  1. Place the character model in the scene.
  2. Add Rigidbody and Capsule Collider.
  3. Add PhotonView and set the Observable properties.
  4. If necessary, set up an animation controller to manage the character animations.

4. Implementing Player Movement and Synchronization

Write a script to control player movement, synchronizing each player’s position and orientation. Below is an example of the PlayerController script.


using UnityEngine;
using Photon.Pun;

public class PlayerController : MonoBehaviourPunCallbacks
{
    float speed = 5.0f;
    void Update()
    {
        if (!photonView.IsMine) return;

        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        transform.position += movement * speed * Time.deltaTime;
        
        // Rotate character based on movement direction
        if (movement != Vector3.zero)
        {
            transform.rotation = Quaternion.LookRotation(movement);
        }
    }
}

4.1 Synchronization and Interpolation Handling

To add synchronization between player characters to the above code, you can add PhotonTransformView to automatically synchronize each player’s position and rotation data.

5. Implementing the Attack System

To handle attacks between players, we will use raycasting to determine hit and create mechanisms for attack handling.

5.1 Setting up Attack Animation

Set the character’s attack animation using the Unity animation system. The animation should play when attacking and detect collisions with enemies.

5.2 Implementing Attack Logic


using UnityEngine;
using Photon.Pun;

public class Attack : MonoBehaviourPunCallbacks
{
    public float attackRange = 1.0f;
    public LayerMask enemyLayer;

    void Update()
    {
        if (!photonView.IsMine) return;
        
        if (Input.GetKeyDown(KeyCode.Space))
        {
            AttackEnemy();
        }
    }

    void AttackEnemy()
    {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.forward, out hit, attackRange, enemyLayer))
        {
            // Handle hit
            Debug.Log("Enemy hit: " + hit.collider.name);
        }
    }
}

6. Handling Multiplayer and Synchronization

When each player performs an attack, the results need to be synchronized over the network. We will use RPC (Remote Procedure Call) to invoke specific methods for all players.

6.1 Synchronizing Attacks through RPC


[PunRPC]
public void PerformAttack()
{
    // Play attack animation
    // Handle hit
}

6.2 Calling RPC


void AttackEnemy()
{
    // Hit logic
    photonView.RPC("PerformAttack", RpcTarget.All);
}

7. Handling Game Over and Results

To automatically synchronize all players’ lives and game over status, you need to write a script to manage each player’s state. In this step, we will set victory or defeat conditions and transmit the results over the network.

7.1 Managing Lives and Game Over Handling


public class GameManager : MonoBehaviourPunCallbacks
{
    public int playerLives = 3;

    public void PlayerDied()
    {
        playerLives--;
        if (playerLives <= 0)
        {
            photonView.RPC("GameOver", RpcTarget.All);
        }
    }

    [PunRPC]
    public void GameOver()
    {
        Debug.Log("Game Over");
        // Game result handling code
    }
}

8. Conclusion

Through this tutorial, you have learned how to implement player synchronization and attack systems in a multiplayer game using Unity. Each step helps to enhance your understanding of network programming and game logic processing. By leveraging Unity's features, you can develop more sophisticated games. Now you can further advance your multiplayer project with custom characters and attack mechanisms.

I hope this tutorial will be helpful for your multiplayer game development in Unity. If you have additional questions or want more in-depth content, please leave a comment. Wishing you much success in your game development journey!

Unity Basics Course: What is an Object?

Unity is a very popular engine in modern game development that provides features to create games across various platforms. This tutorial aims to cover the basics of Unity centered around the concept of ‘Object’. An ‘Object’ is the basic unit that makes up a game in Unity, consisting of various elements such as 3D models, textures, sounds, and more. Understanding this is the first step toward effective game development.

1. Definition of Object

An object generally refers to a data structure that exists in a specific area of memory. An object is made up of attributes and methods, where attributes represent the state of the object and methods define the actions that the object can perform. In Unity, this concept of objects is applied in the form of ‘GameObject’.

2. Structure of GameObject

A GameObject is a fundamental component of Unity, and it primarily includes information about its position, rotation, and scale. All GameObjects possess the following basic elements:

  • Transform Component: Specifies the position, rotation, and scale of the GameObject.
  • Renderer Component: Responsible for the visual representation of the GameObject. This includes 3D models, sprites, etc.
  • Collider Component: Used to detect collisions between GameObjects.
  • Scripts: Code that defines the behavior of the GameObject.

3. Creating a GameObject

Creating a GameObject within Unity is very simple. Follow these steps to create a new GameObject:

  1. Click on GameObject in the top menu of the Unity Editor.
  2. Select the type of GameObject you want. (e.g., 3D Object > Cube)

Your scene will now have a cube created, which will include the Transform component by default. You can adjust the cube’s position, rotation, and scale through this component.

4. Adding Components

A GameObject initially only contains the Transform component, but you can add additional components for extra functionality. Here’s how to add components:

  1. Select the created GameObject in the Scene view.
  2. Click the Add Component button at the bottom of the Inspector panel.
  3. Search for or select the component you wish to add.

For example, adding a Box Collider allows the GameObject to detect collisions.

5. Writing Scripts for Objects

You can define the behavior of GameObjects by writing C# scripts. Here is a simple script example:


// CubeController.cs
using UnityEngine;

public class CubeController : MonoBehaviour
{
    void Update()
    {
        transform.Rotate(Vector3.up * Time.deltaTime * 50);
    }
}

        

By adding this script to the cube, it will rotate with every frame. Similarly, you can endow various behaviors to objects in this way.

6. Utilizing Prefabs

Prefabs are templates for GameObjects and provide a pattern that can be reused multiple times. Here’s how to create a prefab:

  1. Select a GameObject and drag it into the project view.
  2. Using the added prefab in the project view, you can create multiple instances in the scene.

Using prefabs allows you to manage GameObjects efficiently and place identical elements in multiple locations.

7. Unity and Object-Oriented Programming (OOP)

Script writing in Unity follows the principles of Object-Oriented Programming (OOP). It supports concepts such as classes, objects, inheritance, and polymorphism, allowing you to write more hierarchical and flexible code using these principles.

For example, you can create a base enemy character class and implement various enemy characters by inheriting from it. This maximizes the advantages of OOP, such as code reusability and easier maintenance.

8. Relationship between Scenes and Games

GameObjects are placed within Scenes, and scenes represent specific states of the game. In Unity, each scene can exist independently and express specific situations with various GameObjects.

A scene can represent a single level or game stage, and by combining multiple scenes, you can provide diverse gameplay experiences. Transitioning between scenes makes it possible to offer various experiences to the player.

9. Maintaining Object State

To maintain and track the state of GameObjects, various data structures can be utilized. Unity allows for data storage and retrieval through PlayerPrefs, JSON files, databases, and more.

If you save the progress of the game, player scores, character positions, and so on, these elements contribute to ensuring continuity in the game and creating a richer gaming experience.

10. Conclusion

Objects and GameObjects in Unity are essential elements of game development. Understanding the definition, structure, creation, and management of GameObjects enables clear and efficient game development. If you have acquired basic knowledge about GameObjects through this tutorial, I encourage you to challenge yourself with more complex game development in the future. The ability to compose and control each object will be the cornerstone for creating innovative games using the Unity engine.

References

Unity Basics Course: Creating Functions

In this lecture, we will delve into how to create functions in Unity. Functions are one of the most fundamental elements of programming, designed to perform specific tasks within a code block. In Unity, functions are essential for writing game logic and performing various tasks.

1. Basic Concept of Functions

A function is a code block that takes input values, processes them, and returns output values. A function consists of the following components:

  • Function Name: An identifier used to call the function.
  • Parameters: The values that the function takes as input. These values are used within the function.
  • Return Value: The value returned by the function after completing its task, which is not mandatory.
  • Function Body: The code that defines the task the function will perform.

1.1 Necessity of Functions

Functions enhance code reusability and improve readability. If you need to perform the same task multiple times, using functions allows you to keep your code concise.

2. Creating Functions in Unity

In Unity, functions are defined within the MonoBehaviour class. MonoBehaviour is the base class that allows Unity objects to operate in the game.

2.1 Creating a Basic Function

The following code is an example of creating a simple function:

using UnityEngine;

public class MyScript : MonoBehaviour
{
    void Start()
    {
        // Function call
        int result = AddNumbers(5, 10);
        Debug.Log("Result: " + result);
    }

    int AddNumbers(int a, int b)
    {
        return a + b;
    }
}

In the code above, we defined a function named AddNumbers. This function takes two integer parameters and returns their sum. We call this function within the Start method to print the result.

2.2 Parameters and Return Values

By adding parameters to a function, you can pass values from the outside, allowing the function to behave differently based on those values. Additionally, return values can be utilized by the caller to access the function’s results.

2.3 Functions with Various Return Types

Return types can be various forms, such as void, int, string, etc. Below is an example of a function that returns a string:

string Greet(string name)
    {
        return "Hello, " + name + "!";
    }

The above function takes a name as a parameter and generates a greeting to return.

3. Calling Functions

To call a function, append parentheses after the function name and input the parameters. A function that does not take parameters can simply be called without any inputs.

3.1 Calling Without Parameters

void Hello()
    {
        Debug.Log("Hello!");
    }

    void Start()
    {
        Hello(); // Call
    }

3.2 Calling With Parameters

int result = AddNumbers(3, 7); // Call with parameters

4. Function Overloading

In Unity, function overloading allows you to define functions with the same name but with different parameter types or counts. The following is an example of overloading:

int Add(int a, int b)
    {
        return a + b;
    }

    float Add(float a, float b)
    {
        return a + b;
    }

    void Start()
    {
        Debug.Log(Add(5, 10));      // Integer addition
        Debug.Log(Add(5.5f, 10.1f)); // Floating-point addition
    }

5. Local Variables and Global Variables

Variables declared within a function are called local variables, and they cannot be accessed outside of the function. Conversely, member variables of a class are global variables and can be accessed from all methods within the class.

5.1 Example of Local Variables

void MyFunction()
    {
        int localVar = 10; // Local variable
        Debug.Log(localVar);
    }

5.2 Example of Global Variables

public class MyScript : MonoBehaviour
    {
        int globalVar = 20; // Global variable

        void Start()
        {
            Debug.Log(globalVar);
        }
    }

6. Lambda Expressions and Anonymous Functions

In Unity, you can use lambda expressions to create simple functions in a short code block. An anonymous function is a function for data processing that does not have a name.

Action square = x => Debug.Log(x * x);

    void Start()
    {
        square(5); // Prints 25
    }

7. Error Handling and Functions

Errors can occur within the code of a function, and to handle this, you can use a try-catch block.

void Divide(int a, int b)
    {
        try
        {
            int result = a / b;
            Debug.Log("Result: " + result);
        }
        catch (DivideByZeroException e)
        {
            Debug.LogError("Cannot divide by zero: " + e.Message);
        }
    }

8. Unity Events and Functions

In Unity, you can set up events that allow users to call functions based on specific events. For example, a function can be executed when a button is clicked.

Conclusion

In this lecture, we explored the basic concepts of creating functions in Unity, along with practical code examples. Functions play a very important role in game development, enhancing reusability and readability. This allows for a more structured management of complex game logic.