Question by
SgtTicken · Oct 03, 2016 at 05:42 AM ·
collisioninstantiate prefabexplosionprojectiles
How to only make one explosion spawn on impact.
I am currently making a 2D game using 3D objects (with an orthographic camera) in which the player shoots projectiles that explode on impact with things in the environment. My problem is that it spawns two explosions when shot into corners where two walls come together, probably because it collides with both walls simultaneously. How can I make it only spawn one explosion on impact with a corner? Here is the code I am using for spawning explosions on impact:
public class OnCollideExplode : MonoBehaviour {
public GameObject explosionPrefab;
public GameObject explosionParticle;
private GameObject InstantiatedExplosion;
private GameObject InstantiatedExplosionParticle;
void OnCollisionEnter( Collision collision ){
// Rotate the object so that the y-axis faces along the normal of the surface
ContactPoint contact = collision.contacts [0];
//get contact point with ground and projectile
Quaternion rot = Quaternion.FromToRotation (Vector3.up, contact.normal);
Vector3 pos = contact.point;
//getting position of contact
InstantiatedExplosion = (GameObject)Instantiate (explosionPrefab, pos, rot);
//spawn explosion at contact point
// Destroy the projectile and explosion after spawning explosion
Destroy (gameObject);
InstantiatedExplosionParticle = (GameObject) Instantiate (explosionParticle, pos, rot);
Destroy (InstantiatedExplosionParticle, .3f);
Destroy (InstantiatedExplosion, .2f);
}
}
Thanks!
Comment
Answer by SgtTicken · Oct 03, 2016 at 10:16 PM
Fixed it by putting an if statement around the whole thing that only fired when a boolean was false.
Your answer
![](https://koobas.hobune.stream/wayback/20220612093914im_/https://answers.unity.com/themes/thub/images/avi.jpg)