0
0
Unityframework~5 mins

Particle System component in Unity

Choose your learning style9 modes available
Introduction

The Particle System component helps you create cool effects like fire, smoke, or rain in your game. It makes many small images move and change to look like natural things.

You want to show fire coming from a torch in your game.
You need smoke rising from a chimney or explosion.
You want to add falling snow or rain to make the scene lively.
You want sparkles or magic effects around a character.
You want to create dust clouds when a character runs or lands.
Syntax
Unity
Add a Particle System component to a GameObject in Unity.

You can control it using the Inspector or by scripting:

// Example to start the particle system in C#
ParticleSystem ps = gameObject.GetComponent<ParticleSystem>();
ps.Play();

You add the Particle System component from the Unity Editor by selecting a GameObject and clicking Add Component > Effects > Particle System.

You can control many settings like emission rate, speed, size, color, and lifetime of particles.

Examples
This code gets the Particle System on the current object and starts it playing.
Unity
// Create a Particle System in the Editor and start it
ParticleSystem ps = gameObject.GetComponent<ParticleSystem>();
ps.Play();
This code stops the particles from emitting and clears them.
Unity
// Stop the Particle System
ParticleSystem ps = gameObject.GetComponent<ParticleSystem>();
ps.Stop();
This changes the color of new particles to red.
Unity
// Change particle start color
var main = ps.main;
main.startColor = Color.red;
Sample Program

This script controls a Particle System on the same GameObject. It starts the particles when the game begins. Pressing the space bar will toggle the particle system on and off, and messages will show in the console.

Unity
using UnityEngine;

public class SimpleParticleController : MonoBehaviour
{
    private ParticleSystem ps;

    void Start()
    {
        ps = GetComponent<ParticleSystem>();
        if (ps != null)
        {
            ps.Play();
            Debug.Log("Particle system started.");
        }
        else
        {
            Debug.Log("No Particle System found on this GameObject.");
        }
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (ps.isPlaying)
            {
                ps.Stop();
                Debug.Log("Particle system stopped.");
            }
            else
            {
                ps.Play();
                Debug.Log("Particle system started.");
            }
        }
    }
}
OutputSuccess
Important Notes

Remember to add a Particle System component to your GameObject before using this script.

You can customize the particle effect in the Unity Editor to get different looks.

Use the Debug.Log messages to see what is happening when you press keys.

Summary

The Particle System component creates many small moving images to simulate effects like fire or smoke.

You add it to a GameObject and control it with the Inspector or scripts.

You can start, stop, and change particle properties using simple C# commands.