Search Unity

Question Attaching a VFX to multiple players

Discussion in 'Visual Effect Graph' started by v7342659018, Mar 24, 2023.

  1. v7342659018

    v7342659018

    Joined:
    Feb 17, 2018
    Posts:
    12
    I have a VFX that I want to follow a player. This VFX spawns 1 particle that lasts for a long time, and I want to anchor its position to the player's position.

    One way of achieving this is to child the VFX graph to the player transform directly, and then I will be able to move the VFX around. But if I have multiple players, then I will need to create a VFX graph for each player and attach them separately.

    Is there a way to have a single VFX graph, where I can spawn multiple instances of that VFX in the same graph, and each instance of the VFX will be anchored to a separate position?

    Also somewhat related, is there a way to create a "line renderer" effect, where the transform of the VFX is defined by 2 endpoints (e.g. drawing a line between 2 players)?
     
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,314
    Why is that problem for you?
    Your particles need to follow the player, so you must update their position or even rotation every frame and to do this you must provide that data to that vfx. You would need to collect all positions and send them to GPU with graphics buffer, then read that buffer in your graph and update those particles. It is possible, however setup is not very handy as you need to keep everything in sync.
    This can be done in multiple ways, but in general you just need particle strip and spawn particle(s) between two points to create line. Check Line node or Set Position: Line.
     
  3. v7342659018

    v7342659018

    Joined:
    Feb 17, 2018
    Posts:
    12
    I saw from this thread that separate VFX graphs aren't batched and can be a performance issue. I haven't done any profiling yet, but eventually this might become a problem. For using the graphics buffer, how can I access the it in the VFX graph?

    I'm guessing since I need this line to be drawn between 2 players, this is only possible using the above graphics buffer to pass along the player transforms?
     
  4. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,314
    From 2022.2 or 2022.1 simulation and rendering can be batched in some way. In any case you need to profile first, because it all depends how many instances are there and where is your bottleneck. Also if you have less than 20 players on your scene it might be not worth to overcomplicate things, you need to remember there might be sorting problem too. Don't optimize until it's a real problem.

    There is sample buffer node for that. https://forum.unity.com/threads/vfx-graph-siggraph-2021-video.1198156/

    No, you can expose two position/vector properties in graph inspector and use them as start + end of line. Obviously in this case you can render only one line per vfx. The other way is to pass data via event payload - for example position and target position and use the them as start and end point, but these points can't be updated and will be static and you could only spawn one per event. If you use buffer you could render many strips at the same time, plus update them or even draw polyline (actually using graph property you could do polyline too).