All Case Studies

Field-of-View Mesh System

April, 2024

A Unity-based dynamic field-of-view (FOV) mesh system for 2D and 3D games, inspired by stealth mechanics. It procedurally generates cone-shaped FOVs, detects obstacles and targets, and provides customizable settings for adaptable gameplay and real-time performance.

Introduction

  • Overview: A Unity-based field-of-view (FOV) procedural mesh system that dynamically creates a cone-shaped FOV, cut off by obstacles, for target detection, adapting to both 2D and 3D games, and includes customization options for gameplay and performance.
  • Motivation: As a fan of stealth games, I enjoy developing these types of game mechanics. While similar assets are available on the Unity Asset Store, I created this system as a personal challenge and learning exercise rather than to compete with existing solutions.

Key Features

  • Procedurally Generated Field-of-View: Creates a dynamic FOV cone-shaped mesh using procedural generation.
  • Adaptive Vision Cutoff: Mesh visibility dynamically adjusts it hits an obstacle or target, visualizing that line of sight is blocked.
  • Target Detection with Events: Events when a target enters or exits the FOV, to trigger other game states or enemy state transitions.
  • Visual Cues for Target Detection: Changes the FOV mesh material when a target is detected within the FOV, providing visual feedback.
  • Flexible Configuration: Offers customizable settings for angle, distance, resolution, and update type (Update, FixedUpdate, or custom interval), making it adaptable for different gameplay scenarios and performance requirements.

Challenges & Solutions

1. Efficient Mesh Generation

  • Challenge: Generating and updating an FOV mesh in real-time while ensuring computational efficiency, especially on mobile and lower-end hardware.
  • Solution: Implemented procedural mesh generation using raycasts from a central origin point. Rays are projected outward at specified angles and distances, with the ray endpoints determining the mesh’s vertices. If a ray hits an obstacle, the mesh is truncated at that point; otherwise, it extends to the maximum ray length. Triangles are formed between consecutive ray endpoints and the origin point to create the mesh. Adjustable parameters for mesh resolution (number of rays) and update frequency (Update, FixedUpdate, or custom intervals) allow the system to scale based on hardware capabilities, ensuring smooth performance in different game scenarios.

2. Obstacle and Target Detection in 2D and 3D

  • Challenge: Accurate detection of obstacles and targets was essential for responsive gameplay, with additional complexity in supporting both 2D and 3D colliders.
  • Solution: The system uses layer masks and raycasts to dynamically detect obstacles and targets. Separate scripts handle 2D and 3D detection, with mesh generation algorithms tailored for 2D and 3D vector math and colliders. This design ensures precise and efficient detection while maintaining compatibility with both 2D and 3D environments.

3. Customizable Gameplay Adaptability

  • Challenge: A static FOV system lacked flexibility, restricting its applicability to various games.
  • Solution: Exposed parameters in Unity’s Inspector (and clear public API) for angle, distance, resolution, and update frequency. This allows designers and developers to adjust the FOV system, using preset or runtime values, for specific gameplay needs and hardware constraints, making it versatile across different games.

Future Development

  • Improved Performance Optimization: Further optimize mesh generation and raycasting calculations to enhance performance on low-end devices, especially when using high mesh resolutions.
  • Adaptive Mesh Resolution: Implement dynamic adjustment of mesh resolution based on proximity to targets or obstacles, allowing for high detail where needed while saving resources elsewhere.

Key Takeaways

Working on this FOV mesh system improved my understanding of procedural mesh generation and optimization strategies in Unity. I enjoyed the challenge of balancing customization with performance and gained valuable insights into Unity’s Mesh API.

Have a Unity Project in Mind?

Let’s chat about how I can help making it happen.

I’ll get back to you the next business day.

Get in Touch