Player attacking all enemies at once

Greetings, im doing a melee damage system, where the player attacks enemies, so i used a collision box on the player to detect every time an enemy approaches and save that enemy in a variable, so far so good
the problem is that when i attack a close enemy, the others that are far away also take damage, and that wasnt supposed to happen

I think you need to add the current enemy to an array of enemies. And the “is trigger” needs to be a variable that is on the enemy actor.

So then you go through the array with a for each loop, and for each enemy that “is trigger”, then you damage them.

You might also have other checks. Like, how far away is the enemy? If they are greater than some distance from player, turn IsTrigger to false.

It helps me a lot to use comment boxes to write out the code in plain english first. Helps visualizing the logic.

i did all the steps you said, but it keeps removing health from all enemies, i will put the blueprint link below for you to see.

Nothing looks wrong to me.

That checkbox is marked false, right?

My next troubleshooting step would be to put a text render component on the enemies that will display above their heads. Set its text to display their Is Trigger status. YOu could jsut run it on tick for testing purposes. That will let you watch in real time what is happening with the variable.

Oops, actually i think I see the problem

on the end overlap, look at what you are doing. You are just running through the array. You aren’t checking the actual actor.

You don’t need to run the for each loop every time. That is only to identify, “who are we going to look at?”

the overlapping is just looking at individual actor and flagging them with new information.

The for each loop is then used to look through all of the enemies to see if they have the flag you are looking for.

So in plain english its like you are saying, " anybody who overlaps, if they are an enemy, flag them. If they end the overlap, take the flag away."
Then, when you attack, you say, “ok, for each of the guys who is an enemy, see if they got the flag.”

got it, but what do you suggest? and what can i use to replace the foreach loop there in the ‘on component end overlap’ event?

you don’t need it.

Grab the “other actor” pin, cast to enemy, and set the flag (the boolean).

Then, when you attack, that’s when you need the for each loop.

Right now on overlap you are just taking the entire array of enemies and toggling the bool on or off. So everytime any enemy overlaps, they all turn on. When one ends overlap, they all turn off.

You need to just target the “other actor” and change his specific flag.

It may not have been necessary to set up the array ahead of time.

but the Other Actor doesnt return the variable “is Trigger”, it doesnt recognize that this actor is part of the Enemy class, so i did it using cast


Yes, you’ll need to cast, same as I’ve done above. Then on blue output pin from the cast node, you should be able to get at whatever variables that actor holds.

but its already like this, if you look at the link i sent, where it shows the entire blueprint

yeah thats where i took the screenshot from. On the end overlap you are not getting the overlapped actor, but instead disabling the trigger for every actor in the array.

Which would make me think you wouldnt do damage to any actor, but I think if you setup the debug text or something similar that will help narrow down where the issue is.

You can just set each actor to put their name and variable status to the log if you wanted to do it that way. Then you can review it after exiting play mode.

We need more info. Like how is the player attacking the AI? With their firsts or weapon.

How I do it is to make a sphere collision on the weapon, then create an event, call it “AnimApplyDamage” and use the node overlappingActors of the sphere collision, loop through the actors it touches and use the node “apply damage” on the other actor.

In the Animation used to attack, I use a Anim Notify at the point where I want the attack to do damage.

In the Anim Blueprint, you can create an event of the anim notify that you cast and call the event.

It’s more work upfront, but it works, and you’ll never have to touch it again if you need to make changes to your animations.

is that initially i didn’t want to mess with animations, i wanted to make it simpler first
the system is very simple, just press a key and it takes the life of the enemy that is nearby, but it is taking it from all the enemies that are far away too, as i explained in the topic

Can’t you set up a way on dealing damage to the closest enemy that’s within a certain radius? Or just using an animation that only deals damage for a few frames throughout the animation?

It’ll be a less of an headache than gthis pulling something out of an array.

like i said before, i dont want to use animations for now
so i think the first alternative would be more viable, what do you suggest?

Well, the only thing that’s left is to attach an box collision component to your character; scale it to your needs; bind your enemies to a single, specific class; and write the code that says “every time that I press this button, any actor of this class takes ‘X’ damage if they are inside this collision component.”

You would have to run a boolean to see if an actor of the enemy class is inside the box, first. After that, you would have to find a way to only inflict the damage on whatever’s inside the box.

But it might be easier to skip the class part if you aren’t worried about friendly fire.

so, but thats more or less what i did, i created a collision box on the player and every time an enemy enters that area it changes the boolean value to ‘true’ and saves that enemy in an Actor type variable, and every time when it leaves the area the boolean is changed to ‘false’ and resets the Actor variable, so when the player attacks it takes this Actor variable and creates a cast in the Enemy class using this Actor variable as object (I put a link at the top where it shows the entire blueprint).
i dont understand a lot of unreal in general, but if i were to guess something i d say it has to do with classes, i created an actor blueprint for enemy and put the character there as texture, and a variable called ‘Health’ , but that variable applies to do all the enemies i place on the map or do each of them have a different Health?
because its like all the enemies on the map have the same health

People generally use two floats or integers for health: one is for the max hp, another is the current hp.

So an attack would be getting your target’s current health and subtracting it by the amount of damage that your doing and telling the engine to set the difference as the target’s current hp.

As for the enemy class, can you try getting the “actor of class” when you’re running the collision check and set that as your target? Otherwise, you’ll be attacking anything that’s from the enemy’s class because classes are mainly used to designate factions and serve as a template if you’re into editing stuff, while actors are specific character models. And the health system needs to be tied to the enemy actor’s blueprints.