Hey all, new programmer for Unreal here!
Primary Question: “How can you make a detection system where players and enemies attack each other but don’t harm each other accidentally when they go to attack, except in valid instances?”
Explained:
I’ve been working for a few days to get this one sorted, but I’ve had no luck.
Following this tutorial series… https://www.skillshare.com/en/classes/Unreal-Engine-5-Blueprints-Multiplayer-2D-RPG-Platformer/149262399/projects
I’ve encountered the issues as following:
When the player’s attack-radius collision box overlaps the enemy’s hitbox, and if the player clicks attack (via standard attack input), it should register a hit by sending damage via AnyDamage. This works.
It should be noted that the enemy’s hitbox is separate from its capsule component due to the way that the character needs to collide with other 2D static world assets.
When the enemy runs into the player’s capsule component (not attack-radius hitbox), the enemy registers a hit on the player using a standard AnyDamage setup. This also works.
The hitbox is intentionally setup to both receive the hit register of the player’s attack-radius while also dealing damage to the capsule component of the player.
Note: damage in this system is registered by the player running a “Get Overlapping Actors” function. If done correctly, when an enemy overlaps the box, and if the input is pressed, the blueprint sequence will grab the enemy and put them in an array (since there can be more than one enemy). If that array has at least one enemy (not zero), then the damage function is applied.
The enemy’s damage works similarly except for with the standard (when overlap begins / ends) with the enemy’s hitbox.
However, whenever the attack-radius box and the enemy’s hitbox overlap, the enemy is able to inflict damage on the player via the player’s attack-radius.
Likewise, whenever the attack input is given, the player will receive damage when attacking the enemy alongside the enemy also taking damage.
The capsule components of each player / enemy are set to Pawn as their collision type, with custom collision filters from there.
The hitboxes for each, the player’s attack-radius and the enemy’s hitbox, are set to World Dynamic, with custom collisions likewise.
I’ve actually found that turning the collision types to other types, like pawns, for the hitboxes of each respective BP class actually worked perfectly.
The problem with this is that the capsule component radius of the enemies are often larger than the enemy themselves, making this an unfair hitbox for both player and enemy. Thus, a second hitbox for the enemy was necessary.
Though I don’t think it’s directly related, all of the functions are running through server-based RepNotifies and other replications so that the visual changes are seen from both the client and server level. I’m of the impression that this is irrelevant though, since the code actually works as intended both ways, except for the fact that the two collisions can’t seem to ignore each other in order to apply damage as intended.
Therefore, I’m inclined to believe that the problem is primarily rooted in the collision channels settings.
→ If not for solving the issue within these hyper-specific conditions, is there any conventional way that programmers construct melee combat systems to register single hits without accidentally getting hit by enemies with passive attack methods?
Willing and ready to post additional screenshots if need be!
Player damage functions:
Enemy damage functions:
1
2
[There’s more as a part of this line, but it’s mostly AI related after the hit; tested an functional.]
Enemy Collisions [Hitbox Component]
Enemy Collisions [Capsule]
Player Collisions [attack-radius / “AttackCollision”]
Player Collisions [Capsule]
- The validity of these checks have been verified using Print String checks that would report back the amount of health decreased when a function was applied.
Apologies for the long post. This one’s been frustrating me to no end though, even though it feels simple.