Search Unity

Question Spawn particles only if the containing object is moving forward

Discussion in 'Visual Effect Graph' started by Meresman, Oct 7, 2022.

  1. Meresman

    Meresman

    Joined:
    Sep 15, 2019
    Posts:
    9
    Hi, I'm not sure it is possible, but I'm trying to spawn particles based on movement but only forwards.
    I already tried Spawn Over Distance which is great but it gives me particles also if the object is moving backwards(-Z).

    Is there a way in VFX graph to get the previous position?

    Anyone has an idea?
    upload_2022-10-7_22-5-39.png
     
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,315
  3. OrsonFavrel

    OrsonFavrel

    Unity Technologies

    Joined:
    Jul 25, 2022
    Posts:
    194
    Morning :)
    So as @Qriva mentioned, the link post will help you get the First-part of your problem.
    The "SpawnoverDistance" Block is working fine, but some "garbage" is created so for performance reason, I suggest that you use the Solution described in the Post. The performance issue should be addressed and you can track it here.

    Now regarding the second part : Only spawning when moving forward , here's a simple Solution.

    The idea is to get, the Local "Velocity" of the Gameobject. This way you should be able to get the Axis that you're interested in (Z from what you said) and just check if it's Positive or Not.
    Will then we able to modulate our SpawnRate based on this Sign.

    Here's my try:
    upload_2022-10-10_11-21-22.png
    I'm using the SpawnOver distance described in the Linked Post.
    The velocity is plugged in a "ChangeSpace" Block (Vector). This way get the LocalVelocity. Be sure that your "ChangeSpace" block is is set to "Vector" and that the "TargetSpace" is local.

    From here , we're gonna use a Dot Product to compare our Local Velocity Vector with our Direction.
    In this case as you asked, the local Forward Z (0,0,1). If you're not familiar with Dot Product, I would suggest that you look at this Video from Freya Holmér or even this simple and Visual Explanation.
    To be quick, the result of the Dot should give us a scalar between -1<>1 . So, Positive Results when, the Local Vel is aligned with our Z-Forward, 0, when the vectors are Perpendicular, and Negative when the Vector going in opposed direction.

    I'm multiplying the Dot result by a very big number and Saturate the result (clamp between 0-1).
    With this I can use it to multiply my "spawn Rate" resulting in a system Spawning particles when moving on the Z-Forward: Unity_CtWaR1OMod.gif

    Note that The Dot Product has been used so that you can easily Change the direction you want to compare.
    An example with the Positive XAxis:
    upload_2022-10-10_11-17-0.png

    If you're sure that you want to Forward-Z, just using a "sign" and Saturate should work.
    upload_2022-10-10_11-18-48.png

    Hope you'll find this helpfull.
    Have a great day.
     

    Attached Files:

    Meresman, PaulDemeulenaere and Qriva like this.