Hi all, I have an interesting programming/logic challenge I don’t specifically need to solve but am interested in solving for a 2.5D indie game I’m building.
I have some melee animations and a sword with a rigid body and collider attached. Picking up collisions is no problem, but here’s the thing: there are different ways to distribute damage. Weapons (and spells, and anything that can damage something) have three types of damage applications:
- full: entire damage amount is dealt to each target
- divide: damage amount is spread evenly between targets
- diminish: damage amount drops off at a rate defined by a formula that outputs a linear decline
I mean there is also single target but it’s not relevant here.
For the 2nd and 3rd types, we have an interesting problem. Say we have 3 enemies closely grouped. A 2H sword is swung and passes through all 3 units. The collision events for each unit fire during Fixed Update (as specified on the OnTriggerEnter docs page) - but of course, at separate times.
By waiting until the melee swing “finishes” (triggered by an animation event) I can collect each collision and at the end of the swing, feed the total amount of enemies into the damage handler which is required to calculate divided or diminishing damage types.
This introduces a delay between when the sword passes through the enemy and when damage is dealt. It’s not game breaking, but it is a bit off putting and will be more noticeable for heavy, slow swinging weapons.
The other approach is to attach a semi circle trigger collider to the attacking unit/player, and at the beginning of the swing, check what units are within that semi circle collider.
This would mean I could trigger the “damage” when each collision occurs, but this introduces another problem: if a player rotates while attacking (which the game in its current state allows) that initial semi circle check will be inaccurate. It also means figuring out a range that “mirrors” the sword’s swing trajectory but doesn’t necessarily match it, we are just approximating, so we loose a bit of skill play here.
Another benefit using this approach is that that all 2H swords (even if a few pixels longer/shorter) would have the same trajectory/range check which makes for a more consistent melee play style across different weapons that share the same type.
If any of you have experience with combat and damage systems and have encountered this problem before, I would love to hear some ideas - it’s my first time building these systems from the ground up.
Enjoy your Xmas holidays peeps (if you have any)!