Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Sep 08, 2016 at 10:00 PM by Noxury for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Noxury · Sep 08, 2016 at 07:11 PM · rigidbodyforcedamageexplosionratio

Decrease ExplosionForce and damage continously by distance

Hi,

I am struggeling on how to apply damage and force by the distance from the center of the explosion. The distance of each founded collider is converted to a ratio, with 0 being at the edge of the explosion radius / away from the explosion center and 1 at the explosion center. The applied force and damage should be multiplied with that value, but I am doing wrong calculations.

This is the OnCollisionEnter function of the Proximity Mine:

  void OnCollisionEnter(Collision hit){
     Collider[] col = Physics.OverlapSphere(transform.position, explosionRadius, detectionLayer);
 
     foreach(Collider c in col){
         float dist = Vector3.Distance(transform.position, c.transform.position);
         float ratio = 0;
         ratio = Mathf.InverseLerp(explosionRadius, dist, ratio);

         if(c.gameObject.layer != LayerMask.NameToLayer("Projectile")){
             c.transform.SendMessage("ReceiveDamage", damage*ratio);
             c.transform.GetComponent<Rigidbody>().AddExplosionForce(explosionPower*ratio, 
             c.transform.position, explosionRadius, 30.0F, ForceMode.VelocityChange);
         }
     }
 }




Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by DavidWatts · Sep 08, 2016 at 09:45 PM

you are using lerp wrong the last argument is meant to be a value between 0 and 1 but in your script it is always 0. try this for the ratio. forget about using lerp and try this for the ratio

 float ratio = Mathf.Clamp01( 1 - dist / explosionRadius );
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 Noxury · Sep 08, 2016 at 10:00 PM 0
Share

thank you sir!

avatar image
1

Answer by maxxa05 · Sep 08, 2016 at 09:55 PM

A simple model for an explosion would be that the strength of the explosion decreases with the Inverse-Square law. It won't be a physically exact model, but close enough for a game. It would be something like:

 float ratio = 1 / (transform.position - c.transform.position).sqrMagnitude;

Note that it won't be at 0 at the edge of your radius (in fact, it won't be exactly zero ever). But if the radius is small enough or the explosion is weak enough, the result at the edge will be pretty small

Edit: if you want to use a Lerp absolutely: it would go like this:

 ratio = Mathf.InverseLerp(0, explosionRadius, dist);
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

Follow this Question

Answers Answers and Comments

67 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Exploide GameObjects Into BrokenParts : Unity 1 Answer

How to calculate force from explosion on a rigidbody 1 Answer

AddExplosionForce 0 Answers

How to I calculate damage when using AddExplosionForce? 0 Answers

Directional Explosion 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