"Hello World" in Unity game engine
Tags: beginners, tutorial, learning, unity3d
Published: 2023-12-11 17:53:02

 Unity is a powerful game development engine that allows developers to bring their ideas to life. In this tutorial, we will walk you through the process of creating a basic 3D game using Unity and C#. We will explain the steps involved and provide a detailed breakdown of the code syntax used, helping you understand what each line of code does. So, let's get started!
 
### **Step 1: Set Up the Project**

1. Download and install Unity if you haven't already.
2. Create a new Unity project.
3. Set the project name and location.

### **Step 2: Create a Simple 3D Scene**

1. In the Unity editor, create a new empty GameObject called "Player" by right-clicking in the Hierarchy panel and selecting "Create Empty".

2. Attach a Rigidbody component to the "Player" GameObject by selecting it and clicking "Add Component". This allows physics-based movement.

3. Create a new script called "PlayerController" by right-clicking in the Project panel, selecting "Create", and choosing "C# Script".

4. Open the "PlayerController" script and let's start coding!

### **Step 3: Writing the PlayerController Script**

_Line 1 - 3: Code Syntax Explanation_

```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
```
These lines of code import the necessary namespaces for the script to work correctly.

_Line 4: Code Syntax Explanation_

```
public class PlayerMovement : MonoBehaviour
```

This line of code defines a public class named "PlayerMovement" that inherits from MonoBehaviour, which is a base class provided by Unity.

_Line 5 - 6: Code Syntax Explanation_

```
Vector3 Movement;
public float Speed;
```
These lines of code declare a Vector3 named "Movement" to store the player's movement direction, and a public float variable named "Speed" to control the movement speed of the player character.

_Line 7 - 9: Code Syntax Explanation_

```
void Start()
{
    
}

```
This method is automatically called once when the game starts. It currently has no code inside it.

_Line 10 - 14: Code Syntax Explanation_

```
void Update()
{
    Movement.x = Input.GetAxis("Horizontal") * Speed * Time.deltaTime;
    Movement.y = Input.GetAxis("Vertical") * Speed * Time.deltaTime;
}
```

The Update method is called once per frame. It captures the horizontal and vertical input from the player using `Input.GetAxis`, which returns a value between -1 and 1 based on the player's input. The movement values are multiplied by the speed and the `Time.deltaTime`, which ensures smooth movement regardless of the frame rate.

_Line 15 - 18: Code Syntax Explanation_

```
void LateUpdate()
{
    transform.Translate(Movement, Space.Self);
}

```
The LateUpdate method is called after all Update methods have been processed. It uses the Translate function of the GameObject's transform component to move the player based on the calculated movement vector. The Space.Self argument specifies that the movement should be relative to the player's current rotation.

### Step 4: Apply the PlayerController Script

1. Drag and drop the "PlayerController" script onto the "Player" GameObject in the Unity editor.
 
2. Press Play to test your game!
 
3. Congratulations! You've successfully created a simple 3D game in Unity using C#. By following the step-by-step guide