Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How do I play the VFX by code?

Discussion in 'Visual Effect Graph' started by dangamer2008, Sep 13, 2023.

  1. dangamer2008

    dangamer2008

    Joined:
    Aug 16, 2023
    Posts:
    9
    I want to make a muzzleFlash for my weapon, and I wanna make that when I press the "shooting" button, it plays the visual effect graph, but I don't know how to make a reference of it on visual studio, I wrote "public VisualEffectGraph muzzleFlash", but it doesn't recognize the VFX, so can someone help me to find a way to play de VFX by code?
     
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    Graph is just asset, it describes the vfx, but it has nothing to do with playing it.
    VisualEffect is the component you use to play the effect. Also here is example how to send event with attributes in case you need it.
     
    OrsonFavrel likes this.
  3. dangamer2008

    dangamer2008

    Joined:
    Aug 16, 2023
    Posts:
    9
    Ok, thanks!
     
  4. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    218
    I've noticed that the Event block defaults to OnPlay as its name, and that the instantiation of the VisualEffect component with this event initiates this event. I found that changing the name to anything other than OnPlay will not initiate the play upon instantiation, but will allow me to trigger it with the name I give it. For example, I change OnPlay to FireProjectile, and then in my code I use that to play the VFX.

    Code (CSharp):
    1.  
    2.             VFXEventAttribute EventSettings = VFX.CreateVFXEventAttribute();
    3.             EventSettings.SetInt("ID", unitid);
    4.             EventSettings.SetVector3("VelocityN", Velocity * 10);
    5.             VFX.SendEvent("FireProjectile", EventSettings);
    6.  

    upload_2023-9-15_12-55-22.png