Color Physics Counter

A real-time WebGL physics simulation demonstrating event-driven UI updates.

Role

Gameplay Programmer

Engine

Unity 6 (URP)

Platform

WebGL

Links

Play on Unity Play

1. Project Overview

The Color Physics Counter is a real-time physics simulation built to demonstrate object interaction, event-driven UI updates, and web deployment. In this project, a spawner continuously drops dynamically generated colored spheres into a custom-built, translucent containment box. As the objects fall into the collection zone, the game instantly detects their properties and tallies them on the screen by color.

2. Architecture: Tag-Based Data Routing

To keep overhead low and avoid heavy component-fetching calls during collisions, I implemented a lightweight Counter.cs script. This script utilizes tag-based evaluation to route data. It ensures only valid game objects are processed and bridges the backend logic with Unity's UI Canvas by strictly updating the Text components only when a successful trigger event occurs.

// Simplified logic for event-driven counting based on tags
private void OnTriggerEnter(Collider other) {
    // Evaluate interacting objects using Unity's tag system
    if (other.CompareTag("Red")) {
        redCount++;
        UpdateUIText(redText, redCount);
    } 
    else if (other.CompareTag("Blue")) {
        blueCount++;
        UpdateUIText(blueText, blueCount);
    } 
    else if (other.CompareTag("Green")) {
        greenCount++;
        UpdateUIText(greenText, greenCount);
    }
}

3. Physics & Trigger Systems

I utilized Unity's physics engine by setting up a centralized trigger volume within the containment box (constructed from 3D cube primitives). The core logic relies on C# OnTriggerEnter events to register collisions. This approach ensures the counting logic triggers perfectly without impeding the physical momentum or bouncing behavior of the falling objects.

4. WebGL & URP Optimization

Successfully deploying the project to Unity Play via WebGL required specialized optimization of the Universal Render Pipeline (URP) for web browser compatibility. This involved modifying global shader settings, managing WebGL graphics APIs, and carefully adjusting color space environments to ensure the 3D scene rendered accurately online without severe performance hits.

5. Retrospective

This project served as an excellent practical application of Unity's event-driven architecture. By keeping the Counter script attached directly to the physical trigger zone rather than the individual spheres or the spawner, the architecture remains highly modular. The experience of debugging and publishing to Unity Play also provided valuable insight into the specific graphics and memory constraints of WebGL builds, ensuring future web-based projects will be structured for immediate browser compatibility.