Particle collision lets particles in a game hit objects or each other. This makes effects like sparks or smoke look real.
Particle collision in Unity
Start learning this pattern below
Jump into concepts and practice - no test required
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.
void OnParticleCollision(GameObject other)
{
Debug.Log($"Particle hit {other.name}");
}void OnParticleCollision(GameObject other)
{
if (other.CompareTag("Enemy"))
{
Debug.Log("Particle hit an enemy!");
}
}void OnParticleCollision(GameObject other)
{
// No collision if particle system is stopped
if (!particleSystem.isPlaying) return;
Debug.Log($"Particle collided with {other.name}");
}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.
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}"); } }
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.
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.
Practice
OnParticleCollision method in Unity's particle system?Solution
Step 1: Understand the role of
This method is a special Unity callback triggered when particles hit other objects in the scene.OnParticleCollisionStep 2: Identify its main use
It is used to detect collisions of particles, allowing you to respond to those events in code.Final Answer:
To detect when particles collide with other objects -> Option DQuick Check:
Particle collision detection = To detect when particles collide with other objects [OK]
- Confusing particle emission with collision detection
- Thinking it changes particle appearance
- Assuming it stops the particle system
OnParticleCollision method in a Unity C# script?Solution
Step 1: Recall Unity's method signature
TheOnParticleCollisionmethod receives aGameObjectparameter representing the object hit by particles.Step 2: Match the correct parameter type
Only void OnParticleCollision(GameObject other) usesGameObjectas the parameter, which is correct.Final Answer:
void OnParticleCollision(GameObject other) -> Option AQuick Check:
Parameter type is GameObject = void OnParticleCollision(GameObject other) [OK]
- Using Collider instead of GameObject
- Omitting the parameter
- Using Particle type which doesn't exist
void OnParticleCollision(GameObject other) {
Debug.Log("Hit: " + other.name);
}
What will happen when particles collide with another object named "Wall"?Solution
Step 1: Understand the method behavior
When particles hit an object,OnParticleCollisionis called with that object asother.Step 2: Analyze the Debug.Log statement
The code prints "Hit: " plus the name of the collided object, which is "Wall".Final Answer:
The console will print: Hit: Wall -> Option AQuick Check:
Collision triggers log with object name = The console will print: Hit: Wall [OK]
- Assuming particle system stops on collision
- Thinking method is never called
- Believing other.name is invalid
void OnParticleCollision(GameObject other) {
int count = other.GetComponent<int>();
Debug.Log(count);
}
What is the problem with this code?Solution
Step 1: Check GetComponent usage
GetComponent<T> expects a Component type, butintis a primitive type, not a component.Step 2: Identify the error cause
UsingGetComponent<int>()causes a compile-time error becauseintis invalid here.Final Answer:
GetComponent<int>() is invalid because int is not a component -> Option BQuick Check:
GetComponent requires Component type = GetComponent<int>() is invalid because int is not a component [OK]
- Thinking OnParticleCollision must return a value
- Confusing parameter type with Collider
- Believing Debug.Log can't print integers
OnParticleCollision to achieve this?Solution
Step 1: Use correct method signature
The method must beOnParticleCollision(GameObject other)to get the collided object.Step 2: Access enemy health and apply condition
Retrieve the health component fromother, check if health > 50, then reduce damage only in that case.Final Answer:
In OnParticleCollision(GameObject other), get the enemy's health component, check if health > 50, then reduce damage accordingly. -> Option CQuick Check:
Conditional damage based on health = InOnParticleCollision(GameObject other), get the enemy's health component, check if health > 50, then reduce damage accordingly. [OK]
- Using wrong method signature without parameters
- Ignoring enemy health condition
- Using Collider parameter instead of GameObject
