- Home /
Question by
VoidPhoenix96 · Feb 18 at 04:00 PM ·
c#unity 5issueexplosion
Why doesn't my explosive barrel code work?
This code worked twice and then never functioned again. Why? It still plays the particle system and destroys the object, but doesn't damage or apply any force to objects with the Target script, a collider, and a rigidbody.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Explosive : MonoBehaviour
{
[SerializeField]
LayerMask damagableMask;
public int damage = 60;
public ParticleSystem explosionParticle;
public void Explode()
{
Instantiate(explosionParticle,gameObject.transform.position,gameObject.transform.rotation);
explosionParticle.Play();
Collider[] hitColliders = Physics.OverlapSphere(gameObject.transform.position,20f,damagableMask);
foreach (var hitCollider in hitColliders)
{
Target target = hitCollider.transform.GetComponent<Target>();
Rigidbody rb = hitCollider.transform.GetComponent<Rigidbody>();
if(rb != null)
{
rb.AddExplosionForce(10f,gameObject.transform.position,20f,4f,ForceMode.Impulse);
}
if(target != null)
{
target.TakeDamage(damage);
}
Destroy(gameObject);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613053209im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to save data to selected save slot 2 Answers
Unity5 filled my SSD? 0 Answers
UIButton function 0 Answers