Unity Cookbook. Particle system
Particle system
Two ways to create a particle system
Built-in particle system
Particle system component in Inspector
Particle system modules
- Manual: Particle System Modules
- Main
- Looping
- Start Speed, Start Size, Start Lifetime
- Simulation Space: World / Local
- Play On Awake
- Emission
- Rate over time
- Shape
- Shape of the particle emitter
- Shape (Circle is good for 2D)
- Radius
- Arc
- Position, Rotation
- Color over lifetime
- You can add a gradient so the particles change color over time
- With the handles on top of the gradient, you can also change alpha value (opacity)
- There’s also modules for changing other properties over lifetime
- Texture Sheet Animation
- If you want your particles to have a sprite animation, use this
- Grid: tell how many cels your sprite sheet has
- Renderer
- Material
- You need to create a new material for your particles (see next slide!)
- Sorting fudge: Bigger numbers mean particles are more probably in front of ofter GameObjects. Yeah.
- Order in layer: Tells the draw order of particles in accordance to 2D layers, like tilemaps.
- Material
Particle material
- Create a new material file with Create > Material
- Set some settings for it.
- For basic sprite particles:
- Shader: Particles/Standard Unlit
- Rendering Mode: Fade or Cutout
- Drag your particle sprite (or sprite sheet for animated particles!) to Albedo
- For basic sprite particles:
Control particle system with code
- How to stop / start emitting particles:
public ParticleSystem particles; if (!particles.isEmitting && Input.GetButtonDown("Fire1")) { particles.Play(); } if (particles.isEmitting && Input.GetButtonDown("Fire1")) { particles.Stop(); }
- To emit 20 particles on command:
if (particles.isPlaying && Input.GetButtonDown("Fire2")) { particles.Emit(20); }
Change properties with code
- You can’t change module properties directly like this:
dustParticles.shape.rotation = somethingNew
- You have to assign a new variable for the module first, and change the variables there:
var shape = dustParticles.shape; shape.rotation = new Vector3 ( 0, 0, Mathf.Atan2(PlayerMove.y, PlayerMove.x) * Mathf.Rad2Deg ); var main = dustParticles.main; main.startSpeed = 2 * PlayerMove.magnitude;
Collision modules
- Collision
- Type: World
- Mode: 2D / 3D
- Lifetime loss: 1 (Die on collision)
- Collider Force (Bounce on collision)
- Collide with: You can change the collision layers so the particles only collide with specific layers.
- (GameObject’s Collision layer does not affect particles!!)
- To run a script on particle collision:
- Send Collision Messages has to be checked
- add callback function OnParticleCollision() (works also in 2D, there is no
OnParticleCollision2D
)
- Triggers
- allows you to access and modify particles based on their interaction with specific colliders
- Add colliders to the List under the Triggers module
- Set Inside, Outside, Enter and Exit to preferred value
- Ignore: does nothing
- Kill: destroys particle
- Callback: calls the OnParticleTrigger() callback on attached scripts (again, there is no
OnParticleTrigger2D
)