How do I make my character move towards an enemy and attack it onCollisionOverlapBegin

I am trying to make MOBA-like controlls, including stuff like auto-attack (which I have)

What I am stuck at is that I need my character to automatically react to targets entering his attack range. So, that when he “sees” an enemy he moves towards the target until it’s he’s in range, and then starts attacking it. Honestly Move to target with it’s acceptance radius would be perfect for what I need, but it seems that can’t work with the player.

What I tried doing instead is having a collision sphere on my player, and using a simple move to actor. My issues is I don’t know how to make my character get in range, before executing attack animations. At the moment he plays it as soon as he starts moving. I tried having a function that compares distances, but when I try looping that it crashes my game.

Here’s what I have currently (The enemy distance checker is running of a timer):

Why wouldn’t that work? There used to be a top-down game template (maybe still is) where you’d click on the ground, and the character would move to there, using Move To.
The only thing you have to do to make this work is to make sure you have a navmesh in the level, and that your movement mode is navmesh pathfinding, not manual movement.

Actually it doesn’t. The top down template uses “Simple move to” as well.


That is what I started with.

“Move to” only accepts AI Controllers as target input, so the only way to use it would be to temporarily reposesses the character, which doesn’t work in my case, as I would need to constantly be juggling the character between controllers.

Using the same approach as the topdown template also doesn’t work well for actors, since the enemy is moving towards me, and when I get his location, my character just runs past the enemy to the place they were at when he first noticed the enemy.

For some reason checking the distance continously with a loop crashes the game.

This post from a former league of legends dev might help you regarding auto attacks (he mentions how he would set it up in Unreal Engine).

https://www.reddit.com/r/unrealengine/comments/x7i4ri/comment/ind17gb/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button

Regarding “Simple Move To”, you might want to run that functionality in Tick, because if the enemy moves it should theoretically keep moving towards the target. After a certain distance within the target, you could tell it to stop moving.

Another idea (which might not work for you depending on how you want certain things) is that you could have your player essentially be controlled by an AI controller, and you as the player would essentially give “commands” to your AI. This would allow your player to use Behavior trees and such.

Does Simple Move to Actor not do what you want?

I think having the character controlled by an AI controller would actually solve a lot of my problems. Could you give me some guideance on how to do that?

Who would the player posses at the begining of the game?

And thanks for that link for the league of legends dev. I will have to try that out as well.

The problems I am having with it is firstly, that the attack animation plays immediately when the character starts moving.

So he makes the first step, attack, and then walks the rest of the way. I would need a way for him to finish one action before starting the next.

The next problem, which is sort of related to the above, is that the target is also moving towards him. So I have to constantly recalculate the distance, and I guess I am not doing that correctly, because my always crashes.

What should happen, is that he walks until he’s in attack range, and only then goes for the attack.

Would you have an idea on how I might continously update the distance between my target and my player?

Calculate it in Tick()?

You can also use an overlapping collider in your player character, like a sphere centered in front of the character, and decide to only attack when an enemy Pawn overlaps that sphere.

That being said, it is totally possible to use an AI controller on a Pawn, and have your Player Controller no possess any Pawn. Update the blackboard/goals for the AI Pawn to control what it’s supposed to be doing. For very indirect gameplay with multiple controlled pawns, (RTS-style) this is probably the most natural. For a MOBA where you only control one pawn, it feels a little more contrived, but you can totally make that work if that’s the way you want to go.

You’ll want to specify “none” for the default pawn class in your gamemode. You’ll want to specify your PlayerAIController as the AI controller class in your player Pawn. You’ll want to set AutoPossess to happen both on spawn and placement in the world. You’ll want to have your PlayerController spawn the Pawn in its Event Begin Play (or some other suitable location) and retain a reference to it, so you can update it with new goals.

I’m sure there’s other people who have put more thought into it, but briefly thinking about it you could have the player essentially possess a spectator pawn that’s attached to your AI pawn (You could take advantage of this to move your camera independently from your character as well if you wish). From there I would look at tutorials on RTS elements because you could essentially just use those systems (You would just be passing commands onto your AI player to follow).