Lesson 7: Collectibles & Power-ups

Welcome to the exciting world of collectibles and power-ups! In this lesson, you'll learn how to create engaging collectible systems that keep players motivated and add depth to your 2D platformer game. We'll design power-up mechanics that enhance gameplay and create memorable player experiences.

Fish🐟 by Dribbble Artist

What You'll Learn

By the end of this lesson, you'll be able to:

  • Design a comprehensive collectible system with multiple item types
  • Implement power-up mechanics that enhance player abilities
  • Create scoring and progression systems that motivate players
  • Add visual and audio feedback for collectible interactions
  • Balance collectible placement for optimal gameplay flow

Why Collectibles Matter

Collectibles are the secret sauce that transforms a good platformer into a great one. They provide:

  • Player Motivation: Clear goals and rewards for exploration
  • Gameplay Depth: Strategic decisions about risk vs. reward
  • Replayability: Reasons to revisit levels and improve scores
  • Progression: Sense of advancement and achievement

Step 1: Design Your Collectible System

Types of Collectibles

Let's create a diverse collectible system with different purposes:

Basic Collectibles:

  • Coins: Standard currency for scoring
  • Gems: Rare, high-value collectibles
  • Stars: Special collectibles for bonus content

Power-ups:

  • Speed Boost: Temporary movement enhancement
  • Jump Boost: Enhanced jumping ability
  • Shield: Temporary invincibility
  • Magnet: Attracts nearby collectibles

Collectible Properties

Each collectible should have:

  • Value: Points or currency gained
  • Rarity: How often it appears
  • Visual Appeal: Attractive sprite and animation
  • Audio Feedback: Satisfying collection sound
  • Persistence: Whether it respawns or is permanent

Step 2: Create the Collectible Script

Create a new C# script called Collectible.cs:

using UnityEngine;

public class Collectible : MonoBehaviour
{
    [Header("Collectible Settings")]
    public CollectibleType type;
    public int value = 10;
    public bool isPowerUp = false;
    public float powerUpDuration = 5f;

    [Header("Visual Effects")]
    public GameObject collectEffect;
    public AudioClip collectSound;

    [Header("Animation")]
    public float floatSpeed = 2f;
    public float floatHeight = 0.5f;
    private Vector3 startPosition;

    private void Start()
    {
        startPosition = transform.position;

        // Start floating animation
        if (type == CollectibleType.Coin || type == CollectibleType.Gem)
        {
            StartCoroutine(FloatAnimation());
        }
    }

    private System.Collections.IEnumerator FloatAnimation()
    {
        while (true)
        {
            float newY = startPosition.y + Mathf.Sin(Time.time * floatSpeed) * floatHeight;
            transform.position = new Vector3(transform.position.x, newY, transform.position.z);
            yield return null;
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            CollectItem(other.gameObject);
        }
    }

    private void CollectItem(GameObject player)
    {
        // Play collection effects
        if (collectEffect != null)
        {
            Instantiate(collectEffect, transform.position, Quaternion.identity);
        }

        if (collectSound != null)
        {
            AudioSource.PlayClipAtPoint(collectSound, transform.position);
        }

        // Apply collectible effects
        ApplyCollectibleEffect(player);

        // Update score
        GameManager.Instance.AddScore(value);

        // Destroy collectible
        Destroy(gameObject);
    }

    private void ApplyCollectibleEffect(GameObject player)
    {
        if (isPowerUp)
        {
            switch (type)
            {
                case CollectibleType.SpeedBoost:
                    player.GetComponent<PlayerController>().ApplySpeedBoost(powerUpDuration);
                    break;
                case CollectibleType.JumpBoost:
                    player.GetComponent<PlayerController>().ApplyJumpBoost(powerUpDuration);
                    break;
                case CollectibleType.Shield:
                    player.GetComponent<PlayerController>().ApplyShield(powerUpDuration);
                    break;
                case CollectibleType.Magnet:
                    player.GetComponent<PlayerController>().ApplyMagnet(powerUpDuration);
                    break;
            }
        }
    }
}

public enum CollectibleType
{
    Coin,
    Gem,
    Star,
    SpeedBoost,
    JumpBoost,
    Shield,
    Magnet
}

Step 3: Enhance Player Controller for Power-ups

Add power-up functionality to your existing PlayerController:

[Header("Power-up Settings")]
public float speedBoostMultiplier = 1.5f;
public float jumpBoostMultiplier = 1.3f;
public float magnetRange = 3f;

private float originalSpeed;
private float originalJumpForce;
private bool hasShield = false;
private bool hasMagnet = false;

private void Start()
{
    originalSpeed = moveSpeed;
    originalJumpForce = jumpForce;
}

public void ApplySpeedBoost(float duration)
{
    moveSpeed *= speedBoostMultiplier;
    StartCoroutine(RemoveSpeedBoost(duration));
}

public void ApplyJumpBoost(float duration)
{
    jumpForce *= jumpBoostMultiplier;
    StartCoroutine(RemoveJumpBoost(duration));
}

public void ApplyShield(float duration)
{
    hasShield = true;
    // Add visual shield effect
    StartCoroutine(RemoveShield(duration));
}

public void ApplyMagnet(float duration)
{
    hasMagnet = true;
    StartCoroutine(RemoveMagnet(duration));
}

private System.Collections.IEnumerator RemoveSpeedBoost(float duration)
{
    yield return new WaitForSeconds(duration);
    moveSpeed = originalSpeed;
}

private System.Collections.IEnumerator RemoveJumpBoost(float duration)
{
    yield return new WaitForSeconds(duration);
    jumpForce = originalJumpForce;
}

private System.Collections.IEnumerator RemoveShield(float duration)
{
    yield return new WaitForSeconds(duration);
    hasShield = false;
}

private System.Collections.IEnumerator RemoveMagnet(float duration)
{
    yield return new WaitForSeconds(duration);
    hasMagnet = false;
}

private void Update()
{
    if (hasMagnet)
    {
        AttractNearbyCollectibles();
    }
}

private void AttractNearbyCollectibles()
{
    Collider2D[] collectibles = Physics2D.OverlapCircleAll(transform.position, magnetRange);

    foreach (Collider2D collectible in collectibles)
    {
        if (collectible.CompareTag("Collectible"))
        {
            Vector2 direction = (transform.position - collectible.transform.position).normalized;
            collectible.GetComponent<Rigidbody2D>().velocity = direction * 5f;
        }
    }
}

public bool HasShield()
{
    return hasShield;
}

Step 4: Create Collectible Prefabs

Coin Prefab

  1. Create a new GameObject and name it "Coin"
  2. Add a SpriteRenderer with your coin sprite
  3. Add a CircleCollider2D (set as Trigger)
  4. Add the Collectible script
  5. Set Type to "Coin", Value to 10
  6. Add a simple rotation script for visual appeal

Gem Prefab

  1. Create a new GameObject and name it "Gem"
  2. Add a SpriteRenderer with your gem sprite
  3. Add a CircleCollider2D (set as Trigger)
  4. Add the Collectible script
  5. Set Type to "Gem", Value to 50
  6. Add a glow effect or particle system

Power-up Prefabs

Create similar prefabs for each power-up type:

  • SpeedBoost: Red icon, value 0, isPowerUp = true
  • JumpBoost: Blue icon, value 0, isPowerUp = true
  • Shield: Yellow icon, value 0, isPowerUp = true
  • Magnet: Purple icon, value 0, isPowerUp = true

Step 5: Implement Scoring System

Create a GameManager.cs script to handle scoring:

using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    [Header("UI References")]
    public Text scoreText;
    public Text coinText;
    public Text gemText;

    private int score = 0;
    private int coins = 0;
    private int gems = 0;

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }

    public void AddScore(int points)
    {
        score += points;
        UpdateUI();
    }

    public void AddCoin()
    {
        coins++;
        UpdateUI();
    }

    public void AddGem()
    {
        gems++;
        UpdateUI();
    }

    private void UpdateUI()
    {
        if (scoreText != null)
            scoreText.text = "Score: " + score;

        if (coinText != null)
            coinText.text = "Coins: " + coins;

        if (gemText != null)
            gemText.text = "Gems: " + gems;
    }

    public int GetScore()
    {
        return score;
    }

    public int GetCoins()
    {
        return coins;
    }

    public int GetGems()
    {
        return gems;
    }
}

Step 6: Level Design Best Practices

Collectible Placement Strategy

Easy to Find (80%):

  • Place coins along the main path
  • Use coins to guide players toward objectives
  • Place power-ups in safe, accessible locations

Medium Challenge (15%):

  • Hide gems behind obstacles
  • Place power-ups in slightly risky locations
  • Create optional paths with valuable collectibles

Hard to Find (5%):

  • Secret areas with rare collectibles
  • Collectibles requiring advanced techniques
  • Easter eggs for dedicated players

Visual Hierarchy

  1. Coins: Common, low value, simple design
  2. Gems: Uncommon, medium value, more attractive
  3. Stars: Rare, high value, very attractive
  4. Power-ups: Distinctive colors and shapes

Step 7: Audio and Visual Polish

Collection Effects

  • Particle Systems: Sparkles, coins, or custom effects
  • Screen Shake: Subtle camera shake on collection
  • UI Feedback: Score pop-ups, coin counters
  • Audio: Satisfying collection sounds

Power-up Indicators

  • Visual Effects: Glowing auras, screen effects
  • UI Elements: Power-up icons with timers
  • Audio Cues: Distinctive power-up sounds
  • Player Feedback: Clear visual changes

Mini Challenge: Create 5 Different Collectibles

Your task is to create 5 different collectible types with unique properties:

  1. Basic Coin: 10 points, common, simple animation
  2. Rare Gem: 50 points, uncommon, glowing effect
  3. Speed Boost: Temporary speed increase
  4. Jump Boost: Enhanced jumping ability
  5. Magnet Power: Attracts nearby collectibles

Requirements:

  • Each collectible must have distinct visual design
  • Implement proper scoring and UI updates
  • Add audio feedback for each type
  • Create particle effects for collection
  • Test power-up duration and balance

Pro Tips for Collectible Design

Engagement Strategies

  • Risk vs. Reward: Place valuable collectibles in challenging locations
  • Exploration Rewards: Hide collectibles in secret areas
  • Skill Progression: Require advanced techniques for rare items
  • Visual Teasing: Show collectibles that require planning to reach

Balancing Guidelines

  • 80/15/5 Rule: 80% easy, 15% medium, 5% hard to find
  • Value Progression: Increase value with difficulty
  • Power-up Duration: Balance temporary vs. permanent benefits
  • Respawn Logic: Decide which collectibles respawn

Player Psychology

  • Completionist Appeal: Give players reasons to collect everything
  • Skill Expression: Reward player mastery with rare collectibles
  • Exploration Motivation: Create curiosity about hidden areas
  • Achievement Satisfaction: Make collection feel rewarding

Troubleshooting Common Issues

Collectibles Not Being Collected

  • Check Tags: Ensure player has "Player" tag
  • Collider Setup: Verify trigger colliders are enabled
  • Layer Settings: Check collision matrix settings
  • Script References: Ensure Collectible script is attached

Power-ups Not Working

  • Duration Issues: Check coroutine timing
  • Reference Problems: Verify player controller references
  • State Management: Ensure power-up states are properly tracked
  • UI Updates: Check that UI elements are properly referenced

Performance Issues

  • Object Pooling: Reuse collectible objects instead of destroying
  • LOD System: Reduce effects for distant collectibles
  • Audio Management: Limit simultaneous audio sources
  • Particle Limits: Control particle system complexity

What's Next?

In the next lesson, we'll dive into Audio & Visual Effects to polish your game with sound design, particle effects, and visual enhancements that bring your 2D platformer to life.

You'll learn to:

  • Design immersive audio landscapes
  • Create satisfying visual feedback
  • Implement particle systems for impact
  • Add screen effects and camera work
  • Balance audio and visual elements

Key Takeaways

  • Collectibles drive engagement through clear goals and rewards
  • Power-ups add strategic depth and temporary gameplay changes
  • Scoring systems motivate players to improve and explore
  • Visual and audio feedback make collection satisfying
  • Balanced placement creates optimal gameplay flow

Related Resources


Ready to make your game irresistible? Start by creating your first collectible and watch how it transforms player engagement. Share your collectible designs in our community and get feedback from fellow developers!

Previous Lesson: Lesson 6: Enemies & AI Behavior
Next Lesson: Lesson 8: Audio & Visual Effects