Hello, I’m currently competing in a gamejam and trying to make a fairly simple ai that roams around then when senses the player, walks up and damages them. This does work almost perfectly but sometimes I’m randomly taking damage when no enemies are around and I can’t figure out how. At first it seemed like they were damaging me as soon as they saw me. Here’s the code of the player and ai related to this. Thank you
Hello Meshii and thank you for posting.
I have attempted to recreate your BP and was unable to reproduce the error.
Since you stated you’ve received damage when no AI were around, I would perform additional tests. In the BPs that can cause damage to the player, I made sure to set self as the damage causer. Then in the Player BP, I added a print string, using the Damage causer value of the Event Any Damage as the context. So at least now, when I am hit, I can see who damaged me. This may be somewhat useful for you. while testing.
I hope this information helps.
Hello @Meshii. I think I know why this is happening. You are calling the ApplyDamage method after the navigation of the enemy returns a success. Now, you may think this will happen only when the enemy reaches the player, but in reality it is successful if it reaches the end of the path that was generated.
There is a significant difference there. If the player character is unreachable by your enemy but it is seen by it, then the AI will generate a path to the closest reachable point in the nav mesh to the current player’s location and will return “Success” when it reaches the end of that path. In practice, this will mean that the enemy will deal damage to you each time it reaches the end of the path that was generated at a given time, which may be many more times than you would expect.
As a solution, I would suggest moving the call of the ApplyDamage function into a different place. Maybe have it on Tick and check the current distance to the player before attacking and add a timer so that it will not be called on every tick that it is close to the player, for example, attack after 2 seconds from last attack.
Hope this helps, good luck!
Thank you for your reply, I think you’re pretty much spot on I’m just struggling to implement that. I’ve added this code to test but it’s only ever triggering “not close” even in range and out of it.
@Meshii Hey, it might be because your acceptance radius (the maximum distance to apply damage) is not enough. Remember Unreal measures distance in centimeters and 5 centimeters is close to nothing. Specially considering that you are counting from the center of one actor to the center of the other (assuming that the root of the actor is inside their collision volume).
Still, you could have your damage code in the Tick function, because what you have now should work at the time that the enemy reaches you, but if you stay in proximity to him he wont try to attack you again unless it generates a new path to follow.
Here is an example of what I mean. But be careful that this code doesn’t consider a timer between attacks. You should definitely add one so that you don’t apply damage on each tick.
If your question was resolved please mark this thread as resolved