id: 241 slug: vr-game-level-event-logic-programming title_en: "Programming Event Logic of VR Game Levels" tags: [vr-ar]
Programming Event Logic of VR Game Levels
Event logic of VR-level — not scripting triggers in OnTriggerEnter. This is narrative order management, environment state, NPC reactions and educational hints in 3D space where player can look anywhere, stand wherever and interact with objects in arbitrary order. Linear script breaks on third step: player picked up item before dialog triggered — system doesn't know state.
Where Simple Event Logic Breaks in VR
Most common problem — state serialization via boolean flags. bool doorOpened, bool npcGreeted, bool puzzleSolved in GameManager. With 15 flags combinatorial bugs start: flag npcGreeted set, but doorOpened no, yet user already inside — because passed through wall on re-entry. Debug without explicit state model almost impossible.
Second case — concurrent events. Player takes key and simultaneously steps on door trigger. Both events fire in one frame, OnTriggerEnter and XRGrabInteractable.SelectEntered execute in undefined order. If logic doesn't handle order — state desynchronizes.
Third problem specific to VR-trainers: mandatory step skip. Instructor designed mandatory action order, but in VR user physically can perform step 5 before step 2. Need system either soft-blocking premature actions or adapting scenario to actual user action order.
Event System Architecture for VR-Levels
Foundation — Level State Machine with explicit states and transitions. Not flags, but enum LevelState { Introduction, PuzzleActive, DoorUnlocked, Completed } with TryTransition(LevelState target) methods.
For complex non-linear scenarios use Hierarchical State Machine or Behavior Tree:
-
BehaviorDesigneror custom BT for NPC-reactions - For overall level logic — custom
LevelOrchestratoronIEnumerator-coroutines orUniTask
Event Bus — central event broker. All level components publish events to bus, unaware of each other: EventBus.Publish(new KeyPickedUpEvent(keyId)). LevelOrchestrator subscribed to needed events and updates State Machine. Breaks direct dependencies between Trigger-components and Orchestrator.
Implement via event Action<T> or ScriptableObject-based EventChannel (pattern from Unite Austin 2017): [CreateAssetMenu] KeyPickedUpEventChannel : EventChannelBase<KeyPickedUpEvent>. Each EventChannel — separate asset, links via inspector — not via Find().
Checkpoint system — for trainers critical: each completed step serialized to SessionData. On re-run or resume — state restored exactly. Save not flags, but State Machine snapshot + completed events list with timestamps.
VR-Narrative Specifics
In VR player looks where wants, so classic "narrative event center screen" doesn't work. Need mechanisms for soft gaze direction:
Spatial Audio Cue: sound from point of interest — player turns naturally. Implemented via AudioSource with 3D spatial blend + ReverbZone.
Peripheral Attention Trigger: bright effect (particles, light) in peripheral vision — works better than pointing UI arrow.
NPC Look At: NPC looks at player and starts dialog only when player looks at him (angle < 45°). Checked via Vector3.Dot(playerHeadForward, directionToNPC). Prevents situation where dialog starts "behind back".
From case study: in VR-trainer for fire safety, player must execute 7 evacuation steps strictly in order. Initially logic on flags — testing 40% users found way to "break" scenario. After rewriting to explicit State Machine with TryTransition() — order validation became part of architecture, not if-check collection. Completion rate without errors grew from 55% to 89%.
Debugging and Tools
Custom Level State Viewer — Editor Window showing current State Machine state real-time in Play Mode. List of active events, transition history, pending events queue. Without this tool, event logic debugging — Debug.Log() in dark.
Event Log: all events recorded with timestamp and stack trace in ring buffer. On bug — instant answer "what happened before".
Work Stages
Scenario analysis. Break down level logic, identify states, events, dependencies.
State Machine design. States and transitions diagram before code.
Development. EventBus, Orchestrator, VR-components integration (XR Interaction Toolkit events).
Debug tools. Level State Viewer, Event Log.
Testing. All edge cases: concurrent events, step skip, re-run.
| Scale | Estimated Timeline |
|---|---|
| Linear scenario, 5–10 events | 1–2 weeks |
| Non-linear level, 20–40 events | 3–5 weeks |
| Complex trainer with BT and checkpoint system | 2–4 months |
Cost calculated after scenario breakdown and transition logic complexity assessment.





