Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by podmaster · Jul 09, 2014 at 09:28 PM · damageboundsexplosion

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

alt text

untitled-1.jpg (60.2 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image podmaster · Jul 11, 2014 at 09:00 PM 0
Share

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

spellscript .txt (4.6 kB)

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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 ;)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image podmaster · Jul 10, 2014 at 12:10 AM 0
Share

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 ;)

avatar image Bunny83 · Jul 10, 2014 at 01:51 AM 0
Share

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.

avatar image
0

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.

https://www.youtube.com/watch?v=KK2XHyObKik

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image podmaster · Jul 10, 2014 at 01:30 AM 0
Share

i can't use Physiscs, i have to use Physics2D

avatar image
0

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.

Diagram02


sin título-1.jpg (71.7 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

FPS Tutorial Rocket Launcher 4 Answers

Explosion damage script only deals damage in the first explosion and none afterwards 0 Answers

How to I calculate damage when using AddExplosionForce? 0 Answers

Decrease ExplosionForce and damage continously by distance 2 Answers

how to make explosion with particles do damage 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges