The Problem:
In my journey of enemy AI development, this week I did some housekeeping. After implementing the different attack styles, an attacking and passive state, and other base level AI functionality, I decided it was time to add some user feed back to the combat loop. For this sprint I decided I was going to add in a hurt and damaged animation.
An issue arose when trying to convert my implemented blueprints to C++. See, in the blueprint editor, under AI perception, there are built in stimuli types such as sight, sound and damage. The issue is I only know how to check for sight in code. I tried several different techniques, even going so far as trying a simple if else check i.e. if the enemy receives stimuli and its not from sight then its damage, play hurt animation etc. That didn’t work because again, when I damaged the enemy, it was not being alerted, or in this case, damage being dealt was not being detected by the AI Perception system. Implementing death was relatively simple. When the enemy AI’s health reached zero, I would play a montage and wait till it was done before calling this->Destroy(). In order to get the hurt animation to play, I had to get clever and determine if there was another means or method to check when the enemy AI was taking damage. That being said, there is a part two to this problem. If and when I play the hurt animation, I need to ensure that all behavior tree task nodes stop while the animation is playing. At this point I was really starting to feel the pressure as I only had so much to time to complete this task before the sprint was over.
<Reference Image of initial AI Perception function to switch states>
The Solution:
So, this story, really ends in with like a 90% solution. Through a combination of Google and YouTube, I discovered a built in function that gets called when the enemy takes damage called… “takeDamage()”. This was perfect! Every time I hit the enemy this function was called. Step one complete. From there I proceeded to add in all the other additional functionality on the enemy base. This consisted of three functions: one to start the animation, one to start a timer that would check when the montage was over, and the third broadcasting the animation was complete.
<Reference Image of the TakeDamage() and where I am now setting the state>
From there all that was left to do was to create a behavior tree task node that would call the animation function when the node was hit and to wait until the event dispatcher was called before sending the success result. After some play testing and the addition of a few flags, I got a somewhat functioning enemy AI.
When I hit the enemy AI, the AI will execute the hurt animation no problem. The issue I later discovered is when you spam the hurt animation. Because I am merely playing a montage and have yet to create a specific state machine in the animation blueprint, the enemy AI gets stuck in the animation and doesn’t reset upon taking more damage. Note taken and I will add it to the backlog for future fixes but for the time being I got an enemy AI that plays a hurt animation when it takes damage and plays a death animation when its health reaches 0.
<Reference Image of the hurt animation>
<Reference Image of death animation>
Did you like this post? Tell us
Leave a comment
Log in with your itch.io account to leave a comment.