Game Logic¶
Core Gameplay¶
The game follows a Whack-a-Mole mechanism:
- Moles appear from randomized hole positions
- The player clicks on visible moles to score points
- Score updates in real time with combo-based multiplication
- Missed clicks reset the combo streak
- Game speed increases as time decreases
- Hit feedback is enhanced using camera shake
- Gameplay supports start menu, pause, resume, and restart flow
System Components¶
Mole Spawning¶
- Randomized hole-based spawning using a
holesarray - Coroutine-based pop-up and hide cycle
- Variable pop speed controlled by
GameManager - Hide duration changes dynamically as difficulty progresses
- Only active moles are enabled depending on selected difficulty
Interaction Detection¶
- Raycast-based click detection on visible moles
- Background click detection through
MissDetector - Input is ignored when the game is paused
- Input works only while the game is active
Scoring System¶
- Base score is awarded for each successful hit
- Combo multiplier increases when hits happen within a short reset window
- Final score added per hit is calculated as
amount * combo - Combo is reset on missed clicks
- Score and combo text are updated on the UI in real time
Sound System¶
- Pop-up sound when a mole appears
- Whack sound when the mole is hit
- Miss sound for background clicks
- Combo sound when combo streak increases
- Menu background music, button click sound, start sound, and game-over sound
Difficulty Adjustment¶
- Three selectable difficulty levels:
- Beginner
- Challenger
- Insane
- Each difficulty changes:
- Pop speed
- Maximum speed
- Hide time
- Number of active moles
- High score is stored separately for each difficulty
Pause and Session Control¶
- Start menu panel shown before gameplay begins
- Game can be paused using the Escape key
- Pause panel provides Resume and Restart options
- Restart returns the user to the start menu and resets game state
- Resume and restart button flows use realtime waiting so sound can play even when paused
Visual Feedback¶
- Camera shake is triggered on successful mole hits
- Hit effect includes squash animation before the mole hides
- Feedback improves the responsiveness and feel of the gameplay loop
Game Logic Implementation¶
Core Scripts¶
The gameplay logic is implemented using four primary Unity C# scripts:
1. GameManager.cs¶
Handles:
- Score tracking
- Timer countdown system
- Combo system and combo reset timing
- Difficulty selection and scaling
- Per-difficulty high score saving using
PlayerPrefs - Start menu, pause panel, and game-over panel management
- Resume and restart button handling
- Menu background music and gameplay audio transitions
- Camera shake trigger on successful hit
- Game state checks using
IsGameActive()andIsPaused()
Location: GameManager.cs
2. MoleMovement.cs¶
Handles:
- Random hole selection system
- Coroutine-based mole pop-up and hide animation
- Dynamic speed adjustment from
GameManager - Click detection using
Physics2D.Raycast - Hit animation using squash effect
- Whack sound and pop-up sound effects
- Score triggering via
GameManager - Camera shake trigger on hit
- Auto-stop behavior when game ends
- Pause-aware interaction handling
Location: MoleMovement.cs
3. MissDetector.cs¶
Handles:
- Detection of missed clicks on the background
- Raycast-based click validation
- Combo reset on invalid click
- Miss sound feedback
- Operation only when game is active
Location: MissDetector.cs
4. CameraShake.cs¶
Handles:
- Camera shake effect for successful hits
- Coroutine-based shake animation
- Repositioning the camera back to its original location
- Use of
Time.unscaledDeltaTimeso shake works consistently
Location: CameraShake.cs
Gameplay Flow¶
flowchart TD
A[Start Menu] --> B[Difficulty Selection]
B --> C[Start Game]
C --> D[GameManager Setup]
D --> E[Enable Active Moles]
E --> F[Mole Spawn Coroutine Starts]
F --> G{Player Action}
G -->|Hit Mole| H[Add Score]
H --> I[Increase Combo]
I --> J[Play Whack Sound]
J --> K[Trigger Camera Shake]
K --> L[Hide Mole]
G -->|Miss Click| M[Play Miss Sound]
M --> N[Reset Combo]
F --> O{Pause Pressed?}
O -->|Yes| P[Pause Menu]
P --> Q[Resume or Restart]
Q -->|Resume| F
Q -->|Restart| A
L --> R{Timer Finished?}
N --> R
R -->|No| F
R -->|Yes| S[Game Over Panel]
S --> T[Save High Score]
T --> U[Return to Menu / Restart]
Diificulty Scaling Logic¶
The difficulty system affects both game speed and the number of active moles.
Beginner¶
- Slower pop speed
- Longer hide time
- 1 active mole
Challenger¶
- Medium pop speed
- Reduced hide time
- 2 active moles
Insane¶
- Fast pop speed
- Very short hide time
- 4 active moles
This scaling helps keep gameplay replayable and more engaging across skill levels.
Supporting Systems¶
Combo System¶
- Tracks consecutive successful hits
- Multiplies score depending on streak length
- Shows combo text only when combo is greater than 1
- Hides combo text automatically after a short duration
- Resets on a miss
UI Panels¶
- startMenuPanel
- pausePanel
- gameOverPanel
These panels help manage the full gameplay session flow from menu to end screen.
Audio Management¶
The game uses a shared audio system to handle:
- Menu background music
- Start game sound
- Button click sound
- Game over sound
- Combo sound
- Whack sound
- Pop-up sound
- Miss sound
This improves the feedback quality and makes the prototype feel more polished.
Summary¶
The updated gameplay logic now goes beyond a basic whack-a-mole loop and supports:
- Dynamic mole spawning
- Combo-based scoring
- Miss detection
- Pause and restart support
- Difficulty selection
- Per-difficulty high scores
- Camera shake feedback
- Improved audio and UI flow
These updates made the game more interactive, polished, and technically complete compared to the earlier version.