I am preparing a 2D game I want to cause a surface explosion with damage / destruction of objects within the circle I am using Physics2D.OverlapCircleAll and method call this cause a huge drop (in Profiler included in the category - Others). Can I ask for a hint on how to solve this problem?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShellDestroy : MonoBehaviour
{
private Vector3 explosionPos;
[SerializeField] private float radius=30.0f;
BuildingDestructionController DestControl;
void Start()
{
explosionPos = this.transform.position;
}
void Explode2() {
Collider2D hit = Physics2D.OverlapCircleAll (explosionPos, radius);
foreach (Collider2D c in hit) {
if (c.GetComponent()) {
c.GetComponent().TakeDamage(200);
}
}
}
void OnCollisionEnter2D (Collision2D coll) {
Explode2();
}
}
The TakeDamage method deletes several objects (2-10):
private void DestroyStructure()
{
if (DestroySound != null)
DestroySound.Play();
GetComponent<BoxCollider2D>().enabled = false;
if (DamagedSprite != null)
DamagedSprite.enabled = false;
if (DestroyedSprite != "")
{
GetComponent<SpriteRenderer>().sprite = DestroyedSpriteList[Random.Range(0, DestroyedSpriteList.Length)] as Sprite;
//GetComponent<SpriteRenderer>().color = new Vector4(1, 1, 1, 0.5f);
GetComponent<SpriteRenderer>().sortingOrder = 1;
GetComponent<SpriteRenderer>().sortingLayerName = "Background";
mat_tree = new Material (Shader.Find ("Sprites/Default"));
gameObject.GetComponent<Renderer>().material = mat_tree;
AstarPath.active.Scan();
}
else if (DestroyedSprite == "")
GetComponent<SpriteRenderer>().enabled = false;
if (Particles != "")
particles.Play(includeChildren);
}
public void TakeDamage (int amount) {
if(isDestroyed==true)
return;
HP -= amount;
Strength -= amount;
if (HP <= 0 || Strength <= 0) {
ps.Play(includeChildren);
playeddest = true;
//isDestroyed==true;
DestroyStructure();
}
}