Build a Simple Snake Game in Unity 2D (Beginner Friendly Step-by-Step Guide)
If you’re just starting your journey in game development, building a Snake game is one of the best ways to learn. It’s simple, fun, and teaches you the core concepts of Unity like movement, collision, spawning objects, and score systems.
In this tutorial, we will create a complete Snake game from scratch using Unity 2D. Don’t worry if you have never coded before — everything will be explained in a very simple way.
Why Start with a Snake Game?
The Snake game is perfect for beginners because it covers all the important basics without being overwhelming.
- Simple mechanics
- Easy to understand logic
- Teaches real game development concepts
- Quick to build and test
By the end of this tutorial, you will have a working game and a strong foundation in Unity.
Step 1: Setup Your Unity Project
First, open Unity Hub and create a new project.
- Select 2D Core Template
- Name your project: SnakeGame
- Click Create
Once the project opens, save your scene as MainScene.
Step 2: Create the Snake Head
The snake will start with just a head.
- Right-click in Hierarchy → 2D Object → Sprite → Square
- Rename it to SnakeHead
- Change its color to green
- Set scale to (1,1,1)
Add components:
- BoxCollider2D
- Rigidbody2D (set Gravity Scale = 0)
This ensures smooth movement without falling.
Step 3: Create Movement Script
Create a new C# script called SnakeMovement and attach it to SnakeHead.
using UnityEngine;
public class SnakeMovement : MonoBehaviour
{
public float moveSpeed = 5f;
private Vector2 direction = Vector2.right;
void Update()
{
if (Input.GetKeyDown(KeyCode.W)) direction = Vector2.up;
if (Input.GetKeyDown(KeyCode.S)) direction = Vector2.down;
if (Input.GetKeyDown(KeyCode.A)) direction = Vector2.left;
if (Input.GetKeyDown(KeyCode.D)) direction = Vector2.right;
}
void FixedUpdate()
{
transform.Translate(direction * moveSpeed * Time.fixedDeltaTime);
}
}
This script allows the snake to move in four directions.
Step 4: Create Food Object
The snake needs food to grow.
- Create a new Sprite → Circle
- Rename it to Food
- Change color to red
- Add CircleCollider2D (Is Trigger = true)
Position it randomly in the scene.
Step 5: Detect Collision with Food
Update your SnakeMovement script to detect food.
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Food"))
{
Destroy(collision.gameObject);
}
}
Don’t forget to tag your Food object as Food.
Step 6: Spawn Food Again
We need food to respawn after being eaten.
Create a new script called FoodSpawner:
using UnityEngine;
public class FoodSpawner : MonoBehaviour
{
public GameObject foodPrefab;
public void SpawnFood()
{
float x = Random.Range(-8, 8);
float y = Random.Range(-4, 4);
Instantiate(foodPrefab, new Vector2(x, y), Quaternion.identity);
}
}
Call this function whenever food is eaten.
Step 7: Make Snake Grow
Now we make the game more interesting — growing snake.
Create a body part prefab (small square) and write logic to add segments.
Basic idea:
- Store body parts in a list
- Add new segment when food is eaten
- Move body parts behind the head
This is the core mechanic of Snake.
Step 8: Add Boundaries (Game Over)
Create invisible walls around your game area.
- Add BoxCollider2D to walls
- Detect collision
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Wall"))
{
Time.timeScale = 0;
Debug.Log("Game Over");
}
}
Tag walls as Wall.
Step 9: Add Score System
Create a simple score counter.
public int score = 0;
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Food"))
{
score++;
Debug.Log("Score: " + score);
Destroy(collision.gameObject);
}
}
You can later display this on UI.
Step 10: Improve Gameplay Feel
Now your basic game is ready. Let’s improve it.
- Add sound effects
- Use smooth movement grid system
- Add restart button
- Improve graphics
These small changes make your game feel more professional.
Beginner Tips
- Keep your code simple
- Test frequently
- Break problems into small parts
- Don’t rush learning
Game development is a skill — it improves with practice.
Common Mistakes
- Making movement too fast
- Forgetting to use FixedUpdate
- Not tagging objects correctly
- Skipping debugging
These mistakes are normal — learn from them.
Conclusion
Building a Snake game might seem simple, but it teaches you the foundation of game development — movement, collision, spawning, and game logic.
Once you understand these basics, you can create more complex games with confidence.
The best way to learn Unity is to build, experiment, and keep improving.
0 Comments