Finally a very important piece of the combat system has been developed, at least in early stages. The hit detection and reaction. Every time the player performs an attack sequence we want to detect any hits. And every time a hit is detected we want objects to react to it. During this phase we take stamina, deal damage, reduce health. To do hit detection we need to perform several actions.
Hit Detection
First of all on every animation sequence we want to have an animation notify state where we want to detect a hit. We created a custom notify state and added it to all attack sequences. The notify state will execute a function during a given timeline on the animation sequence.
Every time a notify is received during the animation notify state we execute the hit detect function on the player blueprint. This function will perform a sphere trace for objects from start location to end location on a given radius. All objects that end up inside the sphere trace are considered hits. Hits then are broken into target results. On the target results we call the hit reaction event.
To create more precise hit detection only on the equipped weapons we are using socket transforms. On the sword weapon skeleton we added two sockets: start and end. Then on the blueprint we get socket transforms and add them to sphere trace start and end location accordingly. This way the sphere trace will be as long as the sword is and follow its movement.
Hit Reaction
On the master blueprint state we have an event called hit reaction. This event will grab the attached objects health decrease it by a given amount, play a hit react animation montage, check if health is below zero and perform a death animation montage. This event is then called after the break hit result on the player blueprint. We added hit reaction there because we reused the same component on the player blueprint and enemy blueprint. This way both the enemy and the player can react to hits and perform their own functions and animation montages.
Weapon Based Attack Animations
Attack animations were directly embedded on the player blueprint as a select node. This means that no matter what weapon we equip the same animation montages will play. To fix this we created a master weapon blueprint and a struct with animation montage array. On the master weapon blueprint we added the struct. Then created a child blueprint from the master weapon blueprint for the sword. On this blueprint we filled the animation montage array from the parent struct with our desired montages.
This way we can have different weapons with a different list of attack animations. Next time we want to have a halberd or an axe we can simply create a child blueprint from the master weapon blueprint and fill in the animation montages for each of them accordingly. On the player blueprint we get a reference to the animation list and pick a montage based on the attack index.