Hello! In this tutorial, we will learn how to develop a simple player movement function using Unity. Unity is a powerful engine that allows the development of various games and applications, and it is a preferred platform for many developers. This tutorial is aimed at those with a basic understanding of Unity, and it will be explained in detail so that beginner developers can easily follow along.
1. Project Setup
When starting with Unity for the first time, it is important to set up the project. Let’s create a new 3D project following the steps below.
- Open Unity Hub and click the New Project button.
- Select 3D as the project type.
- Name the project “PlayerMovement” and click the Create button.
Once the project is created, the main editor screen will open. Here, you can create scenes and add game objects.
2. Setting Up the Scene
Now, let’s set up a basic scene. We will add a 3D object, a cube, and modify it to create a player character.
2.1 Adding a Cube
To add a cube, follow these steps:
- Right-click in the Hierarchy window and select 3D Object > Cube.
- Once the cube is created, adjust the cube’s Transform settings in the Inspector window to change its position (for example, set it to
(0, 0.5, 0)
).
2.2 Setting Up the Camera
Adjust the camera position so that the player can see the cube clearly:
- Select the Main Camera object in the Hierarchy.
- Set the Transform’s Position in the Inspector to
(0, 2, -5)
. - Set the Camera’s Rotation to
(15, 0, 0)
so that it faces the cube.
3. Creating the Player Movement Script
Now, let’s add a script to allow the player to move. The process of creating and attaching the script to a game object is as follows.
3.1 Generating the Script
- Right-click on the Assets folder in the Project window and select Create > C# Script.
- Name the script
PlayerController
.
3.2 Writing the Script Content
Now, open the PlayerController.cs
file and enter the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5.0f;
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(horizontal, 0, vertical);
transform.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
}
}
3.3 Attaching the Script
Attach the created script to the cube (player object):
- Select the cube in the Hierarchy.
- Click the Add Component button in the Inspector and search for
PlayerController
to add it.
4. Testing Player Movement
Now that all the settings are complete, follow the steps below to test if the player can move:
- Click File > Save in the top menu to save the scene.
- Click the Play button in the top menu to run the game.
- Try moving the player using the arrow keys or WASD keys on the keyboard.
5. Implementing Additional Features
Having just the basic movement feature may not make the game interesting. Therefore, let’s add more features.
5.1 Adding a Jump Feature
Now, let’s add a jump feature. To do this, we need to use the physics engine. In this example, we will add a Rigidbody component to the cube as a collider, and implement the jump feature in the script.
void Start()
{
rb = GetComponent();
}
void Update()
{
...
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
5.2 Interacting with Obstacles
To enable the player to interact with obstacles, collision detection can be implemented. For example, you can add functionality that increases the score when the player reaches a target or collects an item.
6. Final Testing and Improvements
After confirming that all features work correctly, consider adding elements that enhance the fun of the game. For instance, you could improve the graphics or add background music.
7. Conclusion
Today, we learned how to implement player movement functionality in Unity. Based on this basic knowledge, continue to add more features and improve your game. You can become a better developer through practice. Thank you!
8. References
The official Unity documentation and community forums are excellent resources for learning. If needed, visit the following links to find more information: