- Home /
Instantiated object won't collide with particles
I'm working on a flamethrower for a boss, and everything was going well until I tried to make the flamethrower destroy torpedoes launched by the player's ship on contact.
I've already successfully made the flamethrower do damage to the player's ship, and even to other enemy ships. I have also made the flamethrower destroy torpedoes that I manually placed in the editor.
The trouble seems to only affect torpedoes that have been instantiated at runtime.
Is the World Particle Collider unable to detect objects that have been instantiated at runtime?
Here's the script that should be receiving the collision:
using UnityEngine;
using System.Collections;
public class Destructible : MonoBehaviour {
public float health = 100f;
public GameObject explosionPrefab;
private bool destroyed = false;
void Start () {
}
// Update is called once per frame
void Update () {
if(health <= 0 && !destroyed)
{
destroyed = true;
Collider[] nearbyThings = Physics.OverlapSphere(transform.position,200);
foreach(Collider thing in nearbyThings)
{
thing.gameObject.BroadcastMessage("OnNearbyDestruction",gameObject,SendMessageOptions.DontRequireReceiver);
}
if(explosionPrefab)
{
Instantiate(explosionPrefab,transform.position,Quaternion.identity);
}
Destroy(transform.root);
}
}
void OnParticleCollision(GameObject other)
{
if(other.tag == "Fire") health -= Time.deltaTime * 50;
}
}
Answer by aldonaletto · Jul 30, 2011 at 04:37 AM
How are you manually placing the torpedoes? Duplicating scene torpedoes? It looks like your torpedo prefab is different from the ones you place manually - Unity doesn't (or shouldn't) make difference between instantiated objects and duplicated ones.
EDITED: I guess you should remove Time.deltaTime from the OnParticleCollision - this will make the damage proportional to how many particles hit the torpedo.
You must use Time.deltaTime in continuous events to have an fps-independent effect - but as far as I know this event occurs only once for each particle hit, what in conjunction with Time.deltaTime would produce an inverse fps dependency: the slower the frame rate, the greater the damage.
void OnParticleCollision(GameObject other) { if(other.tag == "Fire") health -= 20; // tweak the damage value as needed }
I'm placing the very same prefab I'm instantiating from. I'll experiment some more and see if I can figure out what's going on.
So far, I've found that placing the prefab in the editor before running the game will create a torpedo affected by the flame thrower. However, a torpedo launched by the player is not affected by the flame- and I've also noticed that a torpedo placed in the editor while the game is running is also unaffected by the fire.
I tested instantiated projectiles with a particle collider and it worked fine. Can you post the script which contains OnParticleCollision?
It looks like the collision volume of the particles are quite small, is the problem. The torpedoes appeared to be running into the particles but they only "hit" the particle when it's dead center.
Your answer
Follow this Question
Related Questions
How to control World Particle Collider through script 1 Answer
Respawn Player Using a Coroutine 0 Answers
Change value of a projectile that is instantiated in Awake and pooled 1 Answer
Instantiate Prefab Problem 1 Answer
Dynamically parenting a collider object to a rigidbody object causes collision bug. 1 Answer