Memory Game

This post details the creation of a classic Memory card matching game using Unity’s Entity Component System (DOTS). The implementation challenges players to find matching card pairs by remembering their positions.

Key Components

Card Data Structure

The foundation uses a Card component storing:

  • Value: Internal identifier for matching logic
  • Face: Entity reference to the visual representation

Card Prefab

A companion CardAuthoring class enables inspector-based configuration rather than hard-coding values.

Card Inspector

Game State Management

A GameSettings component tracks:

  • Number of successful matches
  • Currently revealed cards (max 2)
  • Card flip animation duration

Grid Generation

The MatchingGrid system:

  • Dynamically calculates rows/columns based on pair count
  • Creates balanced spatial distribution
  • Assigns matching values and materials to card pairs
  • Randomly positions cards to prevent predictability

Game Mechanics

Card Selection

The CardClickingSystem handles player interaction:

  • Converts mouse position to world coordinates
  • Uses bounding boxes for click detection
  • Prevents selecting more than 2 cards simultaneously
  • Triggers card rotation animations

Card Comparison Logic

After 2 cards are selected:

  • System waits for flip animations to complete
  • Compares internal values
  • Either removes matched pairs or resets them face-down
  • Detects win condition and triggers reset

Rotation Animation

The RotateToTargetSystem uses spherical interpolation to smoothly animate card flips from 0° to 180°, creating fluid visual feedback.

Technical Highlights

The architecture demonstrates ECS benefits: data-driven gameplay logic, efficient component queries, and clean separation of concerns across specialized systems.

Source Code

Full implementation available on my GitHub repository.