Unity 6 are here, and it's more powerful than ever. In this tutorial, we will build a simple "Endless Runner" game to teach you the core concepts of Unity engine, C# scripting, and mobile optimization.
First, go to unity.com and download Unity Hub.
When you open Unity, you'll see several windows:
Right-click in the Hierarchy > 3D Object > Cube. Name it "Player".
To make it move, it needs physics. With the Player selected, look at the Inspector, click Add Component, and search for Rigidbody.
In the Project window, right-click > Create > C# Script. Name it "PlayerMovement". Drag it onto your Player object.
Double click to open it in Visual Studio. Paste this code:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 10f;
public Rigidbody rb;
void FixedUpdate()
{
// Move forward automatically
rb.AddForce(0, 0, speed * Time.deltaTime);
// Move left/right with input
if (Input.GetKey("d"))
{
rb.AddForce(500 * Time.deltaTime, 0, 0);
}
if (Input.GetKey("a"))
{
rb.AddForce(-500 * Time.deltaTime, 0, 0);
}
}
}
Create another Cube, resize it to look like a wall, and place it in front of the player. Name it "Obstacle".
Create a script called "PlayerCollision". We will detect when we hit something:
using UnityEngine;
public class PlayerCollision : MonoBehaviour
{
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.name == "Obstacle")
{
Debug.Log("We hit an obstacle!");
// Restart level logic here
}
}
}
Ready to play on your phone?
Congratulations! You just built the prototype for a hit game. Next, try adding a score system, better graphics from the Asset Store, and sounds.