- Home /
Physics2D.OverlapCircleAll and method call cause a huge drop in animation frames
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();
}
}
Answer by Greg79 · Aug 29, 2021 at 12:05 PM
Solved: AstarPath.active.Scan(); Scanning for new Graph (A* pro) is very expensive :-)
Your answer
Follow this Question
Related Questions
My character is floating. How do I fix this? 1 Answer
Replacing mesh collider with primitive colliders via script 1 Answer
Tilemap Collider 2D 2 Answers
Trouble with fieldInfo.SetValue() and arrays 1 Answer
How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer