Course contentsShow
← Unity Game Development
Lesson 683 of 82810. VFX and Post-ProcessingFree lesson

Triggering VFX Events via Scripting

Control the state of your visual effects from C# scripts to synchronize particle bursts with gameplay events and player actions.

In this lesson, you will learn how to bridge the gap between gameplay logic and the GPU-driven VFX Graph by exposing properties to C# scripts for precise event synchronization.

While VFX Graph simulations run entirely on the GPU for performance, you often need them to react to specific gameplay moments, such as a player’s attack or a sudden environment collapse. To achieve this, we use the VisualEffect component and its property-setting methods.

Setting up the Interface

In the VFX Graph window, open the Blackboard. Create a new property—for example, a float named BurstAmount or a bool named IsTriggered. By exposing these as properties, you create a hook that the Unity engine can modify from the CPU side.

Scripting the Interaction

Once your property is defined, you can manipulate it via a C# script attached to the same GameObject. The UnityEngine.VFX namespace provides the VisualEffect class, which allows you to send data to the GPU in real-time.

using UnityEngine;
using UnityEngine.VFX;

public class EffectTrigger : MonoBehaviour
{
    public VisualEffect vfxComponent;

    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            // Trigger a single-frame burst by sending a signal
            vfxComponent.SendEvent("OnPlayBurst");
            
            // Or, update a property dynamically
            vfxComponent.SetFloat("SpawnRate", 500f);
        }
    }
}

Why This Workflow Matters

By using SendEvent, you can trigger specific nodes within your graph, such as a "Spawn" node configured to trigger only on a custom event signal. This is significantly more efficient than constant polling. When you combine this with SetInt, SetFloat, or SetVector3, you can pass live gameplay data—like a player’s current speed or impact force—directly into your simulation. This ensures that your particles aren't just "playing," but are actively participating in the player’s experience.

Key Takeaway: Use the VisualEffect component's SendEvent and Set methods to bridge CPU gameplay logic with GPU-accelerated particle behavior for perfectly timed visual feedback.