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!