x


Chain lightning attack

I am trying to create a function for a chain lightning attack in a tower defense game and I am unsure of how to prevent the chain attack from bouncing between the two same targets back and forth.

Currently, I am using a collision sphere extending from the first target to get the closest enemy object but using simply this from the second target could potentially cause bouncing behavior. How can I counteract this?

The function is currently being called by a missile object on collision with it's target. I am writing in JavaScript.

more ▼

asked Jul 14 '11 at 02:24 PM

nostagmus gravatar image

nostagmus
16 1 1 2

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

2 answers: sort voted first

Keep a list of targets you've already hit, something like

var hitTargets : List.<GameObject> = List.<GameObject> ();

in your projectile. When you've found a target to jump to, see if the target already exists in your list using

hitTargets.Contains (newTarget.gameObject)

If it's already there, don't jump to it and find the next target that isn't already in your list. When you find that target, add it to the jump to it and add it to the list:

hitTargets.Add (newTarget.gameObject);

The whole check might look something like:

var newTarget : Target = FindTarget (currentTarget); //I'm guessing at the name of your target class/interface, and you'd implement finding a target however you like
while (hitTargets.Contains (newTarget.gameObject))
{
    newTarget = FindTarget (currentTarget);
    if (newTarget == null) // Make FindTarget return null or in some way indicate if there are no more targets to jump to so we know to break out if we can't find a target.
    {
        return;
    }
}

// Once we're out of that loop, we know the target hasn't already been hit.
JumpTo (newTarget); //Do whatever you need to do to move the projectile along.
hitTargets.Add (newTarget.gameObject);
more ▼

answered Jul 14 '11 at 03:18 PM

burnumd gravatar image

burnumd
3.4k 22 35 71

This seems like a valid solution for my problem. I'll try and implement this.

Is there any reason a list is used instead of an array?

Jul 14 '11 at 03:24 PM nostagmus

Lists contain built-in methods for determining whether something is in them (as you can see in the code example) and are pretty easy to work with (you get Add and Remove methods and the like). Using generic lists makes your code more type-safe. As an added bonus, generic lists of a Unity type are serialized, so you can edit them in the Inspector!

Jul 14 '11 at 03:41 PM burnumd

PS, I edited the answer to provide more clarity on what you'd do to check if you've already hit the target.

Jul 14 '11 at 03:49 PM burnumd
(comments are locked)
10|3000 characters needed characters left

Each target has a boolean flag in it, "alreadyHit". When they get hit, set that true. Further checks ignore any target with that set true. When the whole attack is done, set all of these flags to false.

more ▼

answered Jul 14 '11 at 02:38 PM

almo gravatar image

almo
1.8k 3 6 18

It's better to not have to modify the target class if you don't have to. They shouldn't need to have any logic that's weapon specific, so it makes more sense to make the weapon aware of what it's doing rather than making the targets have to handle some special case for a specific weapon.

Jul 14 '11 at 03:20 PM burnumd

I prefer not having to scan a list on each collision.

Jul 14 '11 at 03:22 PM almo
(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:

x159
x38
x15

asked: Jul 14 '11 at 02:24 PM

Seen: 968 times

Last Updated: Jul 14 '11 at 03:49 PM