Developing a Simple 2D Game: "Fruit Collector" in Unity
Learn to create a basic game where the player collects falling fruits using Unity.
Game Overview:
In this tutorial, we will create a simple 2D game called "Fruit Collector." The goal of the game is to control a basket (player) and collect falling fruits. The game will involve basic Unity features like player movement, collision detection, and a scoring system.
Objective:
- Create a new 2D Unity project.
- Implement player movement using arrow keys or 'A' and 'D'.
- Spawn fruits at random positions at the top of the screen.
- Detect collisions between the player and fruits.
- Keep track of the score for each collected fruit.
Step 1: Create the Player (Basket)
Create the Player Game Object
Right-click in the Hierarchy window, go to 2D Object, and select Sprite. Name it Player and resize it to 100x50 in the Inspector.
Assign a Player Sprite
You can either use a simple box shape or import your own basket sprite.
Player Movement Script
Create a C# script called PlayerMovement and attach it to the Player GameObject. Here’s the script for player movement:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 5f;
void Update()
{
float move = Input.GetAxis("Horizontal");
transform.Translate(move * speed * Time.deltaTime, 0, 0);
}
}
This script allows the player to move the basket left and right using the arrow keys or "A" and "D" keys.
Step 2: Create the Fruit Game Objects
Add Fruit Sprites
Use 3 different fruit sprites, like apple, banana, and orange, or create simple colored circles for now. Name them FruitApple, FruitBanana, and FruitOrange.
Fruit Size
Set the size of the fruits to 20x20 in the Inspector.
Step 3: Fruit Spawning Script
Create a C# script called FruitSpawner to spawn fruits at random x-positions at the top of the screen. Attach the script to an empty GameObject named FruitSpawner.
Fruit Spawner Script
using UnityEngine;
public class FruitSpawner : MonoBehaviour
{
public GameObject[] fruits;
public float spawnRate = 1f;
void Start()
{
InvokeRepeating("SpawnFruit", 1f, spawnRate);
}
void SpawnFruit()
{
float xPosition = Random.Range(-3.5f, 3.5f);
Vector3 spawnPosition = new Vector3(xPosition, 6f, 0);
int fruitIndex = Random.Range(0, fruits.Length);
Instantiate(fruits[fruitIndex], spawnPosition, Quaternion.identity);
}
}
This script spawns fruits at random x-positions above the screen at regular intervals.
Step 4: Collision Detection and Scoring
Adding Colliders
Add a BoxCollider2D to both the player (basket) and fruit. Set the fruit’s collider as a Trigger to allow collision detection.
Collision Script
Create a C# script called FruitCollector and attach it to the Player GameObject. This script will detect collisions and increment the score.
using UnityEngine;
public class FruitCollector : MonoBehaviour
{
public int score = 0;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Fruit"))
{
score++;
Destroy(other.gameObject);
Debug.Log("Score: " + score);
}
}
}
Step 5: Displaying the Score
UI Setup
Create a UI Text element to display the score. Update it in the FruitCollector script as the player collects fruits.
using UnityEngine;
using UnityEngine.UI;
public class FruitCollector : MonoBehaviour
{
public int score = 0;
public Text scoreText;
void Update()
{
scoreText.text = "Score: " + score;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Fruit"))
{
score++;
Destroy(other.gameObject);
}
}
}
Bonus Features:
- Add sound effects for fruit collection.
- Implement a simple falling animation for the fruits.
- Display and save the high score.