1
\$\begingroup\$

It's easy enough to use the Unity Particle System to generate snow that falls from the top of a rectangle.

But how do I make snow only fall inside the rectangle? As in, once the snow hits the bottom of the rectangle, I'd like for it to disappear.

The curveball here is that the width of the rectangle can be dynamically resized at times, and I'd want the entire rectangle to always be snowing.

\$\endgroup\$
3
  • \$\begingroup\$ Have you considered using a shader that clips out snow that leaves the bounding box, similar to the one shown here? (You can ignore the vertex abort trick for your case) \$\endgroup\$
    – DMGregory
    Feb 11, 2023 at 13:29
  • \$\begingroup\$ @DMGregory Would a shader be performant? I'm looking for a performant solution, in case I end up porting to mobile... \$\endgroup\$
    – kanamekun
    Feb 12, 2023 at 1:13
  • \$\begingroup\$ The best way to answer that question is to profile it. There are enough variables in play here that any answer I give you could be inaccurate for your specific use case. \$\endgroup\$
    – DMGregory
    Feb 12, 2023 at 1:15

1 Answer 1

3
\$\begingroup\$

You can use a Box Collider to define the volume where particles are drawn and tell the Particle System to destroy particles lying outside it.

The Particle System features the Triggers module that lets you define particle behaviour when they interact with Colliders in the Scene. You can use it to destroy particles when they move outside a Collider of choice (I suggest a Box Collider since you are generating particles from a rectangle):

  1. Add a Box Collider Component to the Particle System GameObject.
    • Alternatively: create a Box GameObject as a child of the Particle System GameObject. (Parenting GameObjects allows you to move the System around the Scene.)
  2. In the Particle System, enable the Triggers module.
  3. Add a new trigger.
  4. Drag and drop the Box Collider Component onto the trigger empty slot.
    • Alternatively: drag and drop the GameObject owning the Box Collider from the Hierarchy (be it a child GameObject or the Particle System GameObject itself).
  5. Set the Outside trigger condition to "Kill" and the remaining conditions to "Ignore".

You can now define a dedicated shape for generating particles and a different one for containing them. In your case, you may want to place the rectangle (Particle System's Shape module) so that it overlaps the top face of your Box Collider.

Here's a quick snapshot from the Inspector:

enter image description here


EDIT: If you want to resize the container volume without manually adjusting the shapes' position, you can use an Editor script to automatically update the emission shape's position based on the container volume size.

\$\endgroup\$
3
  • \$\begingroup\$ Thank you, this is incredible! I got everything working, except the particles aren't being destroyed when they move "Outside" my collider. I will keep playing with it and report back... \$\endgroup\$
    – kanamekun
    Feb 12, 2023 at 1:41
  • \$\begingroup\$ Oh I see, my collider was configured to be Used By Composite... once I turned that off, then it was easy to kill snowflakes exiting the collider! I will add a new collider on the parent object and try it that way! \$\endgroup\$
    – kanamekun
    Feb 12, 2023 at 4:00
  • \$\begingroup\$ Thank you thank you, this works perfectly! \$\endgroup\$
    – kanamekun
    Feb 12, 2023 at 4:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .