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: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.
// 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++