2D Explosion Damage Calculation - 2DCollider Bound

Hi,i’m making a 2D game in which the player shoots spells.
When a fireball explodes execute this code

Physics2D.OverlapCircleAll(Vector2(transform.position.x ,transform.position.y) , RadiusOfDamage );

To get all enemies inside the explosion radius it works fine.
My problem is to apply damage to every enemy by distance, i’m doing it with this code:

var proximity : float = (transform.position - hitColliders*.transform.position).magnitude;*

it uses the position of the spell explosion and the enemy position but if the enemy collider is too big the center position gets too far from the explosion and the spell doesn’t apply the desire damage some times it even applies any damage at all.
i attach an image that reflects the issue
[28938-untitled-1.jpg|28938]*
*

Well, the 2d version seems to miss some quite useful things like Collider.ClosestPointOnBounds or Collider.Raycast which could easily be used to get the required point.

For 2d colliders you might want to use Physics2D.Raycast. Since that might not give you the exact distance you could do 2 raycasts. One from your explosion center to the center of the enemy. From the hit information you can get the hit normal and do another raycast from your explosion center along the inverted normal. If you don’t hit the enemy, use the hit point of the first raycast. If it hits, use the new hit point as it should be closer.

Alternatively you might just attach a 3d BoxCollider as trigger and use ClosestPointOnBounds :wink:

There is many solutions, many depends of how much precision you need.

The simplest is to have in a variable the radius of enemy and simple reduce.

If you have many enemies to set manually you can gets its render bounds and do some math. Remember that bounds are AABB (Axis Align Bound Boxes).

If you have a good collider for each enemy you can simply cast a raycast and check de distance from the collision point.

Last but not least. Your answer is in this video, he compares many explosion types.

I’m about to give up with this, if i raycast and an enemy is on the way, the one in the back don’t get any damage.
The answers that i found doesn’t work in 2D.
Any other idea? if not i will set the explosion damage to a fix percent.
I tried a lot, raycasting, CircleOverlap, CircleCollider2D , but nothing works…
so frustrating , just need to get the hit.point and calculate the distance.

[29027-sin+título-1.jpg|29027]