- Home /
How to retrieve particle collision location in OnParticleCollision
I'm trying to instantiate an explosion prefab at the location of a particle hit. (the particle system is currently shooting a single particle) Currently I have this:
void OnParticleCollision(GameObject other) {
myExplosion = Instantiate (prefabExplosion) as GameObject;
}
Which instances the explosion at 0,0,0
rather than at the collision location. I figure I need to do something like this, but it's 2AM now, and I just ain't gettin it right:
Particle[] particles = other.collider.particleSystem.GetParticles;
then pull the position from particles[1] - but it's bugs galore... Any takers?
Thanks!
I should mention, this script is attached to the particle emitter, and it has a "world particle collider" attached as well.
Answer by mgc90403 · Jan 02, 2014 at 08:28 PM
This worked:
using UnityEngine;
using System.Collections;
public class OnParticleCollisionAlt : MonoBehaviour
{
public GameObject myExplosion;
public GameObject prefabExplosion;
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)
{
Vector3 collisionHitLoc = collisionEvents[i].intersection;
myExplosion = Instantiate (prefabExplosion, collisionHitLoc, Quaternion.identity) as GameObject;
i++;
}
}
}
Answer by MaciekXB · Jun 22, 2021 at 07:13 PM
Here is a recent example in the unity documentation https://docs.unity3d.com/2021.2/Documentation/ScriptReference/MonoBehaviour.OnParticleCollision.html
Answer by andrew-lukasik · Jun 24, 2021 at 11:08 AM
using UnityEngine;
using System.Collections.Generic;
[RequireComponent( typeof(ParticleSystem) )]
public class ParticleSystemCollisionTester : MonoBehaviour
{
public List<ParticleCollisionEvent> collisionEvents = new List<ParticleCollisionEvent>();
ParticleSystem _particleSystem;
void Awake ()
{
_particleSystem = GetComponent<ParticleSystem>();
if( _particleSystem.collision.enabled==false )
Debug.LogError("ParticleSystem has \"Collision\" module disabled");
if( _particleSystem.collision.sendCollisionMessages==false )
Debug.LogError("ParticleSystem has \"Collision\"/\"Send Collision Messages\" disabled");
}
void OnParticleCollision ( GameObject other )
{
int numCollisionEvents = _particleSystem.GetCollisionEvents( other , collisionEvents );
for( int i=0 ; i<numCollisionEvents ; i++ )
{
ParticleCollisionEvent collision = collisionEvents[i];
Vector3 point = collision.intersection;
Vector3 velocity = collision.velocity;
Vector3 normal = collision.normal;
Debug.DrawLine( point , point + normal , Color.white , 1f );
Debug.DrawLine( point , point + velocity , Color.yellow , 1f );
}
}
}
Answer by TobiasWeber · Feb 14 at 11:25 AM
Hello,
does anybody know on how to get the UV / texture coordinate for ParticleCollisionEvent? I need to draw into a texture, based on the intersection point. This is working fine for a plane, as XY map 1:1 to UV but it does not work for more complex meshes.
Tobias