Hi!
In my little project I have a character who’s firing fireballs (just particles)… When the particle effect collides with a wall or anything else it dissapears. I want to spawn an explosion where the particles collide… but I have no success what so ever.
I have struggled with this a couple of days now and now I need your help.
Here’s my particle prefab setup:

Here’s the ParticleCollision.cs:
using UnityEngine;
using System.Collections;
public class ParticleCollision : MonoBehaviour
{
public Transform explosionPrefab;
void OnCollisionEnter(Collision collision)
{
ContactPoint contact = collision.contacts[0];
Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
Vector3 pos = contact.point;
Transform effect = Instantiate(explosionPrefab, pos, rot) as Transform;
}
}
What am I missing?
I guess there’s another way for when detecting when a particle collides with a ColliderBox for example… Maybe if I make the player shoot a small sphere-model (not rendered thou) and spawn a explosion when the sphere collides with a wall or whatever? If so… how do I make the sphere move as fast as the particle effect?
I’m a little confused by your terminology… when you call that prefab a “particle” you’re not referring to the Unity Particle system are you?(Unity - Manual: Particle systems)…
… and that prefab doesn’t have a collider attached to it, so how are you currently making the “particle” disappear on collision?
When my player shoots the fireballs, the code looks like this:
using UnityEngine;
using System.Collections;
public class PlayerAction : MonoBehaviour
{
public Transform particleEffect;
public GameObject shotOrigin;
public GameObject shotSFX;
void Update()
{
bool spell = Input.GetButtonDown("Spell");
Spell(spell);
}
void Spell(bool spell)
{
if(spell)
{
StartCoroutine(PlayEffect());
}
}
IEnumerator PlayEffect()
{
yield return new WaitForSeconds(0.2f);
Transform effect = Instantiate(particleEffect, shotOrigin.transform.position, shotOrigin.transform.rotation) as Transform;
shotSFX.audio.Play();
yield return new WaitForSeconds(1f);
}
}
I figured out how to spawn an explosion… but explosion doesn’t spawn where the particles (the fireball) hit the wall…
using UnityEngine;
using System.Collections;
public class FBallCollide : MonoBehaviour {
public Transform explosionPrefab;
void OnParticleCollision(GameObject other)
{
Rigidbody body = other.rigidbody;
Debug.Log("COLLISION DETECTED!");
if(body)
{
Debug.Log("EXPLOSION!!");
Transform effect = Instantiate(explosionPrefab, other.transform.position, other.transform.rotation) as Transform;
}
}
}
Interested in this. Bump. Trying something similar, in a 2D geometry game.