How to Paint with Particle System in Unity

Unity Engine, renowned for its diverse and dynamic toolset, offers countless ways to create immersive experiences. Among these, the Particle System stands out as a versatile tool for visual effects, from simulating rain and smoke to crafting intricate animations. One less-explored use of this system is painting within a Unity scene. This tutorial introduces an innovative application of the Particle System to simulate the action of painting on surfaces in Unity.

Prerequisites

1. Setting up the Scene

  • Start Unity and create a new 3D project (if haven't yet)
  • Inside the Hierarchy window, right-click and select 3D Object -> 'Plane'. This will act as the canvas.
  • Adjust the plane's position to (0, 0, 0) for consistency.

2. Creating the Particle System

  • Right-click in the Hierarchy window and select 'Effects' -> 'Particle System'.
  • Rename the Particle System to "ParticlePainter".
  • Position the "ParticlePainter" object slightly above the plane.

3. Configuring Particle System for Painting

  • With the "ParticlePainter" object selected, navigate to the Inspector window.

Under 'Particle System', make the following changes:

  • 'Duration': 1
  • 'Start Lifetime': 'Infinity' (this ensures particles don’t die)
  • 'Start Speed': 0 (particles should not move)
  • 'Emission' -> 'Rate over Time': 100 (adjust according to painting density preference)
  • Go to 'Shape' and select "Circle". Adjust the radius to a smaller value to control the brush size.
  • Under 'Renderer', change the Material to a custom material or color to represent the paint.

4. Scripting the Paint Mechanic

Now, a script will be added to control when and where the particles are emitted.

  • Right-click in the Assets window, select 'Create' -> 'C# Script', and name it "ParticlePainterScript".
  • Double-click on the script to open it in an editor, paste the code below inside it, then save the script:
using UnityEngine;

public class ParticlePainterScript : MonoBehaviour
{
    private ParticleSystem painterParticles;
    private bool canPaint = false;

    void Start()
    {
        painterParticles = GetComponent<ParticleSystem>();
    }

    void Update()
    {
        PaintMechanic();
    }

    void PaintMechanic()
    {
        if (Input.GetMouseButton(0))
        {
            if (!canPaint) 
            {
                painterParticles.Play();
                canPaint = true;
            }
        }
        else
        {
            painterParticles.Stop();
            canPaint = false;
        }

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        
        if (Physics.Raycast(ray, out hit))
        {
            transform.position = hit.point;
        }
    }
}
  • Attach the "ParticlePainterScript" to the "ParticlePainter" object in the Unity editor.

5. Adjusting Camera

Adjust the main camera for a top-down view:

  • Set the Position to (0, 10, 0) and Rotation to (90, 0, 0).

6. Test the Scene

  • Click on the Play button.
  • Hold down the left mouse button and move the cursor over the plane. Particles should be emitted, creating a paint-like effect.

Questions & Answers:

  1. Why use the Particle System for painting instead of traditional texture painting methods?: The Particle System offers dynamic real-time feedback, is highly customizable, and can provide unique visual effects. Additionally, it can be more performance-friendly in some situations, especially for temporary paint effects.
  2. Can multiple colors be painted with this method?: Yes, by creating multiple particle systems or adjusting the particle color over time, various color effects can be achieved.
  3. How can brush size be changed dynamically?: By adjusting the 'radius' value of the Particle System's shape in real-time through scripting, the brush size can be varied dynamically.

Conclusion

With this tutorial's end, a new and unique application of the Unity Particle System has been explored. Whether for game mechanics, artistic tools, or innovative experiences, Unity continues to surprise with its versatile capabilities.

Suggested Articles
Working With Unity's UI System
How to Add Zombies to Unity Games
Implementing Particle Effects in Unity
Review of the Unity Asset Store Package - Planet Shader and Shadowing System
How to Make Light Cookies in Unity
Create a Pressure Washer Foam Effect in Unity
How to Create a Horror Game in Unity