- Home /
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[i].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
DONE! I made it, i use an X1 - X2 = X / Y1 - Y2 = Y to get the distance from the Explosion Origin to the collision point.
Here is the code i made to test link text
Answer by Bunny83 · Jul 09, 2014 at 09:45 PM
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 ;)
this helps me to point in the right direction BUT i use the hit.point it works fine now i have other issue when the spell hits the ground and it raycasts if other enemy is on the way it takes the other enemy hit.point. Is there a way to ignore every enemy collider but the specific in use?
By the way this helps me a lot , thanks ;)
Well, that's what usually "Collider.Raycast" is good for. It casts a ray only against that collider. However, as i said in my answer, the 2d colliders don't have such a method.
You can use Physics2D.RaycastAll and iterate through the results and check the collider if it's the one in question. The other alternative is, like a said above, to use additional 3d triggers.
Answer by Sisso · Jul 09, 2014 at 09:53 PM
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).
http://docs.unity3d.com/ScriptReference/Renderer-bounds.html
If you have a good collider for each enemy you can simply cast a raycast and check de distance from the collision point.
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Last but not least. Your answer is in this video, he compares many explosion types.
Answer by podmaster · Jul 11, 2014 at 02:03 AM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613153642im_/https://answers.unity.com/themes/thub/images/avi.jpg)