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 /
avatar image
0
Question by Creeper_Math · Jan 02, 2017 at 06:19 PM · c#mathdamageexplosionequation

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

I have a script that takes all gameobjects in the scene, and if they have a damage detector and is within the radius of the explosion damage, deals damage to them. This works fine for just the first explosion I deal near the enemy, but not anything else.. Note the first explosion is far away from the player, so the player should not be affected by it.... Every explosion has this script on it.

 foreach (GameObject item in EntitiesDataBase.GameObjects) {
             if (Vector3.Distance(item.transform.position, transform.position) < ExplosionDistance) {
                 if (item != null) {
                     
                     if (item.GetComponent<Rigidbody>() != null) {
                         item.GetComponent<Rigidbody>().AddExplosionForce(ExplosionForce, transform.position, ExplosionDistance); // Just incase this is stopping the script with an error.....
                         
                     }
                     
                     if (item.GetComponent<DamageDetector>() != null) {
                         float Damage = 0;
                         float Distance = Vector3.Distance(transform.position, item.transform.position);
                         // Make an equation between distance and damage
                         Vector2 Pos1 = new Vector2(0, MaxDamage);
                         Vector2 Pos2 = new Vector2(ExplosionDistance, 0);
                         float SlopeN = Pos1.y - Pos2.y; // Numerator
                         float SlopeD = Pos1.x - Pos2.x; // Denominator
                         float YIntercept = MaxDamage;
                         float Slope = SlopeN / SlopeD;
                         Damage = Distance * Slope + YIntercept;
                         item.GetComponent<DamageDetector>().Health += -1 * Damage;
                         
                         
                        
                     }
                     
                     
                 }
                 
             }
         }


"Damage Detector" is a universal script I use for detecting damage for other scripts to use. EntitesDataBase.GameObjects is an array that when a gameobject not in the array already is found, it adds it (so each gameobject has a unique ID number, results in some/most gameobjects being null)

The values that I have it set to, incase that has anything to do with it, is the following ExplosionDistance = 20; MaxDamage = 100;

Comment
Add comment · Show 2
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 elenzil · Jan 03, 2017 at 06:14 PM 1
Share

hard to say. i would suggest applying some general debugging practices:

  • put print statements in to confirm that the code is executing the way you think.

  • is the explosion script even being called ?

  • how many items are in your GameObjects collection ?

  • what's the distance from the explosion to the items ?

  • are the explosion and items where you expect ?

  • do the items have those components ?

  • note, if it would be an error for the items to not have those components, you should have an else {} clause which prints an error. that's just good program$$anonymous$$g practice.

unrelated, your code seems odd because line 3 checks if item is null, but line 2 assumes it is not null.

also, your math to compute damage seems a bit more complex than it needs to be. I'd suggest looking into InverseLerp() and Lerp(). eg,

 float normalizedDistance = InverseLerp($$anonymous$$Dist, maxDist, actualDist);
 float damage = Lerp(maxDamage, $$anonymous$$Damage, normalizedDistance);


avatar image Creeper_Math elenzil · Jan 03, 2017 at 08:50 PM 0
Share

@elenzil That unrelated portion is what was going on... Swapped them both and it works perfectly . I guess it was the first enemy that once destroyed, it was stopping all the other explosion script attempts as they got to the null object before it got to the objects it needed to.

I'll take a look into Lerping and InverseLerping to try and simplify my code! THAN$$anonymous$$S!

0 Replies

· Add your reply
  • Sort: 

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

265 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 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 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 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

Problem with calculating damage, value returning 0 1 Answer

How to you make a number squared in C#? 2 Answers

Distribute terrain in zones 3 Answers

Damage sending with grenades. 1 Answer

Multiple Cars not working 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