0
0
Unityframework~5 mins

Particle collision in Unity

Choose your learning style9 modes available
Introduction

Particle collision lets particles in a game hit objects or each other. This makes effects like sparks or smoke look real.

When you want sparks to bounce off walls in a game.
When smoke particles should disappear after touching the ground.
When raindrops hit the ground and create splashes.
When you want fire particles to react when hitting a character.
Syntax
Unity
using UnityEngine;

public class ParticleCollisionHandler : MonoBehaviour
{
    private ParticleSystem particleSystem;

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

    void OnParticleCollision(GameObject other)
    {
        // Code to run when particles collide with 'other'
        Debug.Log($"Particle collided with {other.name}");
    }
}

The method OnParticleCollision is called automatically by Unity when particles hit another object.

You must have a ParticleSystem component on the same GameObject for this to work.

Examples
Basic collision detection that logs the name of the object hit.
Unity
void OnParticleCollision(GameObject other)
{
    Debug.Log($"Particle hit {other.name}");
}
Check if the particle hit an object tagged as 'Enemy' and respond.
Unity
void OnParticleCollision(GameObject other)
{
    if (other.CompareTag("Enemy"))
    {
        Debug.Log("Particle hit an enemy!");
    }
}
Ignore collisions if the particle system is not playing.
Unity
void OnParticleCollision(GameObject other)
{
    // No collision if particle system is stopped
    if (!particleSystem.isPlaying) return;
    Debug.Log($"Particle collided with {other.name}");
}
Sample Program

This script logs a message when the particle system starts and whenever a particle hits another object. Attach it to a GameObject with a ParticleSystem and enable collision in the ParticleSystem settings.

Unity
using UnityEngine;

public class ParticleCollisionExample : MonoBehaviour
{
    private ParticleSystem particleSystem;

    void Start()
    {
        particleSystem = GetComponent<ParticleSystem>();
        Debug.Log("Particle system started.");
    }

    void OnParticleCollision(GameObject other)
    {
        Debug.Log($"Particle collided with {other.name}");
    }
}
OutputSuccess
Important Notes

Time complexity: Particle collision checks run every frame and depend on the number of particles and colliders.

Space complexity: Minimal extra memory is used for collision events.

Common mistake: Forgetting to enable 'Collision' in the ParticleSystem's settings, so OnParticleCollision never triggers.

Use particle collision when you want particles to interact with the environment. For simple visual effects without interaction, you can skip collision to save performance.

Summary

Particle collision lets particles detect and react when they hit objects.

Use OnParticleCollision(GameObject other) method to handle collisions.

Remember to enable collision in the ParticleSystem component for this to work.