Legacy Documentation: Version 4.6(go to latest)
Language: English
  • C#
  • JS
  • Boo

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

MonoBehaviour.OnParticleCollision(GameObject)

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Description

OnParticleCollision is called when a particle hits a collider.

This can be used to apply damage to a game object when hit by particles.

Legacy particle system:
This message is sent to all scripts attached to the WorldParticleCollider and to the Collider that was hit. The message is only sent if you enable sendCollisionMessage in the inspector of the WorldParticleCollider.

	// Legacy particle system example
	// Applies a force to all rigid bodies that are hit by the particle.

function OnParticleCollision (other : GameObject) { var body : Rigidbody = other.rigidbody; if (body) { var direction : Vector3 = other.transform.position - transform.position; direction = direction.normalized; body.AddForce (direction * 5); } }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void OnParticleCollision(GameObject other) {
        Rigidbody body = other.rigidbody;
        if (body) {
            Vector3 direction = other.transform.position - transform.position;
            direction = direction.normalized;
            body.AddForce(direction * 5);
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def OnParticleCollision(other as GameObject) as void:
		body as Rigidbody = other.rigidbody
		if body:
			direction as Vector3 = (other.transform.position - transform.position)
			direction = direction.normalized
			body.AddForce((direction * 5))

Shuriken particle system:
This message is sent to scripts attached to particle systems and to the Collider that was hit.

When OnParticleCollision is invoked from a script attached to a GameObject with a Collider the GameObject parameter represents the ParticleSystem. The Collider receives at most one message per particle system that collided with it in any given frame even when the particle system struck the Collider with multiple particles in the current frame. In order to retrieve detailed information about all the collisions caused by the ParticleSystem ParticleSystem.GetCollisionEvents must be used to retrieve the array of ParticleSystem.CollisionEvent.

When OnParticleCollision is invoked from a script attached to a ParticleSystem the GameObject parameter represents a GameObject with an attached Collider struck by the ParticleSystem. The ParticleSystem will receive at most one message per Collider that is struck. As above ParticleSystem.GetCollisionEvents must be used to retrieve all the collisions incident on the GameObject.

Messages are only sent if you enable /Send Collision Messages/ in the inspector of the particle system collision module.

OnParticleCollision can be a co-routine, simply use the yield statement in the function.

	// collision event script attached to a GameObject
	// applies a force to rigid bodies that are hit by particles
	private var collisionEvents = new ParticleSystem.CollisionEvent[16];

function OnParticleCollision(other : GameObject) { // get the particle system var particleSystem : ParticleSystem; particleSystem = other.GetComponent(ParticleSystem);

// adjust array length var safeLength = particleSystem.safeCollisionEventSize; if (collisionEvents.Length < safeLength) { collisionEvents = new ParticleSystem.CollisionEvent[safeLength]; }

// get collision events for the gameObject that the script is attached to var numCollisionEvents = particleSystem.GetCollisionEvents(gameObject, collisionEvents);

// apply some force to RigidBody components for (var i = 0; i < numCollisionEvents; i++) { if (gameObject.rigidbody) { var pos = collisionEvents[i].intersection; var force = collisionEvents[i].velocity * 10; gameObject.rigidbody.AddForce(force); } } }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    private ParticleSystem.CollisionEvent[] collisionEvents = new ParticleSystem.CollisionEvent[16];
    void OnParticleCollision(GameObject other) {
        ParticleSystem particleSystem;
        particleSystem = other.GetComponent<ParticleSystem>();
        int safeLength = particleSystem.safeCollisionEventSize;
        if (collisionEvents.Length < safeLength)
            collisionEvents = new ParticleSystem.CollisionEvent[safeLength];
        
        int numCollisionEvents = particleSystem.GetCollisionEvents(gameObject, collisionEvents);
        int i = 0;
        while (i < numCollisionEvents) {
            if (gameObject.rigidbody) {
                Vector3 pos = collisionEvents[i].intersection;
                Vector3 force = collisionEvents[i].velocity * 10;
                gameObject.rigidbody.AddForce(force);
            }
            i++;
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	private collisionEvents as (ParticleSystem.CollisionEvent) = array[of ParticleSystem.CollisionEvent](16)

	def OnParticleCollision(other as GameObject) as void:
		particleSystem as ParticleSystem
		particleSystem = other.GetComponent[of ParticleSystem]()
		safeLength as int = particleSystem.safeCollisionEventSize
		if collisionEvents.Length < safeLength:
			collisionEvents = array[of ParticleSystem.CollisionEvent](safeLength)
		numCollisionEvents as int = particleSystem.GetCollisionEvents(gameObject, collisionEvents)
		i as int = 0
		while i < numCollisionEvents:
			if gameObject.rigidbody:
				pos as Vector3 = collisionEvents[i].intersection
				force as Vector3 = (collisionEvents[i].velocity * 10)
				gameObject.rigidbody.AddForce(force)
			i++

	// collision event script attached to a ParticleSystem
	// applies a force to rigid bodies that are hit by particles
	private var collisionEvents = new ParticleSystem.CollisionEvent[16];

function OnParticleCollision(other : GameObject) { // adjust array length var safeLength = particleSystem.safeCollisionEventSize; if (collisionEvents.Length < safeLength) { collisionEvents = new ParticleSystem.CollisionEvent[safeLength]; }

// get collision events for the gameObject that the script is attached to var numCollisionEvents = particleSystem.GetCollisionEvents(other, collisionEvents);

// apply some force to RigidBody components for (var i = 0; i < numCollisionEvents; i++) { if (other.rigidbody) { var pos = collisionEvents[i].intersection; var force = collisionEvents[i].velocity * 10; other.rigidbody.AddForce(force); } } }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    private ParticleSystem.CollisionEvent[] collisionEvents = new ParticleSystem.CollisionEvent[16];
    void OnParticleCollision(GameObject other) {
        int safeLength = particleSystem.safeCollisionEventSize;
        if (collisionEvents.Length < safeLength)
            collisionEvents = new ParticleSystem.CollisionEvent[safeLength];
        
        int numCollisionEvents = particleSystem.GetCollisionEvents(other, collisionEvents);
        int i = 0;
        while (i < numCollisionEvents) {
            if (other.rigidbody) {
                Vector3 pos = collisionEvents[i].intersection;
                Vector3 force = collisionEvents[i].velocity * 10;
                other.rigidbody.AddForce(force);
            }
            i++;
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	private collisionEvents as (ParticleSystem.CollisionEvent) = array[of ParticleSystem.CollisionEvent](16)

	def OnParticleCollision(other as GameObject) as void:
		safeLength as int = particleSystem.safeCollisionEventSize
		if collisionEvents.Length < safeLength:
			collisionEvents = array[of ParticleSystem.CollisionEvent](safeLength)
		numCollisionEvents as int = particleSystem.GetCollisionEvents(other, collisionEvents)
		i as int = 0
		while i < numCollisionEvents:
			if other.rigidbody:
				pos as Vector3 = collisionEvents[i].intersection
				force as Vector3 = (collisionEvents[i].velocity * 10)
				other.rigidbody.AddForce(force)
			i++