- Home /
Is there any alternative to adding an explosion force for 2D since it's not available. If not, does anyone know how to make it to where objects with a tag in a radius will be destroyed?
This is all I could do before I realized I didn't know what I was doing...
using UnityEngine; using System.Collections;
public class BombExplosion : MonoBehaviour { public float explosionDelay; public float explosionRate; public float explosionMaxSize; public float explosionSpeed; public float currentRadius;
bool exploded = false;
PolygonCollider2D explosionRadius;
public Rigidbody2D rb;
void Start()
{
explosionRadius = gameObject.GetComponent<PolygonCollider2D>();
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
explosionDelay -= Time.deltaTime;
if (explosionDelay < 0)
{
exploded= true;
}
}
void FixedUpdate()
{
if (exploded == true)
{
if (currentRadius < explosionMaxSize)
{
currentRadius += explosionRate;
}
else
{
Object.Destroy (this.gameObject.transform.parent.gameObject);
}
explosionRadius.radius = currentRadius;
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (exploded == true)
{
if(col.gameObject.rb != null)
{
Vector2 target = col.gameObject.transform
}
}
}
}
Answer by bilalitani · Aug 16, 2015 at 03:11 PM
If you just want to destroy the objects, try using Physics2D.OverlapCircleAll(position, radius);
This function will return an array of all Collider2D objects it hit within the radius, and you can access their gameobjects.
Your answer
![](https://koobas.hobune.stream/wayback/20220611232001im_/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
[UNET] Objects other than the local player's have a small jitter when moving. 1 Answer
Rotate around a circle 0 Answers
Instantied 2D Prefab Is Invisible 1 Answer