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
4
Question by Jason Mathews · Apr 01, 2010 at 02:28 AM · gameobjectdistancedamageradiusproximity

How to apply explosion/magic/grenade damage to detect objects (enemies) within a given radius, and apply AOE damage to each enemy based on its distance?

I saw a couple of questions along these lines but there's differences.

I'm trying to accomplish a couple of things here. One a enemy detection by searching a radius every so often. Second I'd like to use the same sort of function to create an AOE damage effect. I've already created a GetDistance from two objects. From previous RPG games a function was created called GetFirst and GetNext and was very nice and I'd like to replicate that again.

This code would run on a WHILE going through the game objects one at a time till they return to the first one. Inside this I would use my GetDistance to determine if they can be targeted or not (AOE spells). I have a lot of other uses for this as well.

I know how to find objects by name, but is there a way to just go through each object? (running it as code should be pretty quick as its just running a small function (GetDistance).

Thanks

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

· Add your reply
  • Sort: 
avatar image
16
Best Answer

Answer by duck · Apr 01, 2010 at 09:21 AM

EDIT: Also see this question which expands on the code below, adding tests for whether objects are behind cover.

If your enemies have colliders attached, you can use Physics.OverlapSphere to quickly get your initial list of enemies within a certain radius.

You can then loop through each enemy within this list and determine the relative distance to the player, and apply some level of damage to each enemy within the radius, proportional to its distance.

It would end up looking something like this. It assumes that your enemy objects have both a collider and a script called "Enemy" attached.

In C#:

void AreaDamageEnemies(Vector3 location, float radius, float damage) { Collider[] objectsInRange = Physics.OverlapSphere(location, radius); foreach (Collider col in objectsInRange) { Enemy enemy = col.GetComponent<Enemy>(); if (enemy != null) { // linear falloff of effect float proximity = (location - enemy.transform.position).magnitude; float effect = 1 - (proximity / radius);

         enemy.ApplyDamage(damage * effect);
     }
 }

}

And in Javascript:

function AreaDamageEnemies(location : Vector3, radius : float , damage : float ) { var objectsInRange : Collider[] = Physics.OverlapSphere(location, radius); for (var col : Collider in objectsInRange) { var enemy : Enemy = col.GetComponent(Enemy); if (enemy != null) { // linear falloff of effect var proximity : float = (location - enemy.transform.position).magnitude; var effect : float = 1 - (proximity / radius);

         enemy.ApplyDamage(damage * effect);
     }
 }

}

(untested code, for illustration only!)

Comment
Add comment · Show 3 · 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 SisterKy · Jul 28, 2011 at 09:52 PM 0
Share

the link with the expanded code checking for cover is broken =( Do you happen to be able to find it...? That would be awesome... Greetz, $$anonymous$$y.

avatar image Destran · Mar 26, 2014 at 10:08 AM 0
Share

http://answers.unity3d.com/questions/15838/how-can-i-apply-damage-based-on-a-grenade-explosio.html

Here's the link

avatar image adhochero · Apr 09, 2020 at 08:16 PM 0
Share

perfect thanks that worked great.

avatar image
1

Answer by efge · Apr 01, 2010 at 11:52 AM

Take a look at the reference for the built-in function AddExplosionForce (with JS example, uses also Physics.OverlapSphere).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make an gameobject dissapear when player gets close to it 0 Answers

two objects with same volume of effect; One affect but other do not 0 Answers

How do I let the player interact with objects close to them? 1 Answer

How to damage gameobject via raycast 1 Answer

Destroying the object that I am selecting 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