Reworking Melee Collision


By Will Fox

3/15/2023

Hey everyone!
Today, I will discuss a minor issue I ran into regarding collision for melee. So, this problem started when I created my first AI enemy called the Ghoul - my first dev post was about the Ghoul and very similar to this one. Essentially, the Ghoul's behavior is as follows; The Ghoul spawns and looks for the player. If it sees the player, it chases them. If not, it walks around. When it chases the player, it is programmed to stop at a certain distance and make a melee attack at them. This melee attack is timed so that when the animation begins, it will activate the collision sphere attached to the Ghoul's hand and de-activate once the attack finishes. Although this may sound ~somewhat~ well thought out, there was an overlooked flaw in the logic. This flaw is that the attack can hit the player multiple times in one swing - obviously, we don't want that.


Here is a snippet of the overlap code for the fists

Unfortunately, due to time constraints, we had to move on from the bug to finish some more critical tasks for the build. But this week, I finally came back to it and figured out how to get this melee function working as intended. Firstly, I began working on an enemy called the Necromancer, who summons minions and rains lightning down onto the player. The minions were to act similarly to the Ghoul in that they are supposed to run at the player and hit them with their left and right fists. Since the functionality was the same as the Ghoul, I might as well make it better. So, when I began working on the melee animations, I tackled it the same way I did the Ghoul. If the minion were swinging with its left fist, it would toggle a boolean that would make sure the minion used its right fist for the next attack. However, the main change I made to the collision of the attack was if the player was to get hit by the collision sphere, it would instantly become deactivated.


Luckily, our logic was set up to always activate the collision spheres when the animation begins, so de-activating the sphere at any given time is only temporary and won't cause any further bugs. Now, all that needs to be done is to activate the collision sphere at a time that makes sense during the animation. This can be done effortlessly using animation notifications. By placing a notification at a place inside the timeline that makes sense for a melee attack to actually deal damage - cause it wouldn't make sense to be damaged by an enemy who has their hands at their side - we can make the game a lot more interactive and give the impression that the player was damaged because of the velocity of the fist, not because of a collision sphere. 


Activating the collision sphere at the peak of the swing

This was essentially the entire issue! A super easy and simple solution that drastically improves the "realism" of a game and allows modularity as you can add particles or a warning system to show when an enemy is going to hit.

Leave a comment

Log in with itch.io to leave a comment.