DEV Community

Cover image for "Hello World" in Unity game engine
JoelShane
JoelShane

Posted on

"Hello World" in Unity game engine

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;
Enter fullscreen mode Exit fullscreen mode

These lines of code import the necessary namespaces for the script to work correctly.

Line 4: Code Syntax Explanation

public class PlayerMovement : MonoBehaviour
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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()
{

}

Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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);
}

Enter fullscreen mode Exit fullscreen mode

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

Top comments (3)

Collapse
 
denchyaknow profile image
Dencho

I opened this post expecting a Debug.Log("Hello World"); in the Update loop xD
Good intro Tut mang

Collapse
 
joelshane profile image
JoelShane

Thank god plan worked finally :)

Collapse
 
hmfsharafa profile image
HMF. Sharafa

Good job !!