x


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

more ▼

asked Apr 01 '10 at 02:28 AM

Jason Mathews gravatar image

Jason Mathews
129 6 7 14

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Apr 01 '10 at 09:21 AM

duck gravatar image

duck ♦♦
41.4k 95 152 415

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, Ky.

Jul 28 '11 at 09:52 PM SisterKy
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Apr 01 '10 at 11:52 AM

efge gravatar image

efge
5.1k 5 14 40

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2173
x373
x254
x51
x39

asked: Apr 01 '10 at 02:28 AM

Seen: 5337 times

Last Updated: Jul 28 '11 at 09:52 PM