How do you make a ranged attack in a mouse based top down game?

I am making a game in the top down template that controls like leauge of legends or diablo. Depending on what I click, the character will move, move & attack, or move & pickup objects. The problem is that I cannot get ranged characters to stop moving once they reach their attack range.

I use ray tracing to determine what I am clicking on, and I compare the distance to my character and the clicked on enemy by fetching both of their locations and subtracting one from the other. The there’s a branch - if the character is in range they attack. The part that I am missing is when characters are not in attack range.

I tried using while loops, but those crashed my game every time. And I had multiple failed attemps where the player goes all the way to the enemy before attacking.

Does anyone know of a way to do this?

1 Like

Hard to advise without knowing how you do things currently. But the most basic loop could look like so:

Treat it as a working pseudoscript.

The gist: check the distance to target against a threshold and arrest whatever the character movement component is doing.

1 Like

That looks exactly like what I am trying to do. Thank you very much. I just started a completely new file just to figure this out, so my setup is a fresh topdown template right now.

But, would you mind clarifying a few points?

Is the “Target” that you set after cast to enemy a variable of type actor or something else?

How would you handle the timer? Is it better to set timer by event or function name?

I tried replicating how you setup your function but mine looks different when I call it.


Screenshot_43

1 Like

Here it is of the enemy type but it also does not matter - it will work the same regardless - the purpose is to check whether there is a target worth attacking. In the grand scheme of things it will matter even less as you will need to employ an interface for communication.

How would you recommend that I set the timer? Is it better to set timers by function name or event?

I prefer by event because I have fat fingers and mistype function names. :innocent:


It’s pure.

image

1 Like

Thank you for the quick responses. I tested it with the Event tick and it works. But I am doing something wrong when I try using timers.

Would you mind showing me how the timer should be setup?

1 Like

I have been stuck on this for almost a week, and you helped me solve this in 20 minutes.

I really appreciate your help and your quick responses. Thank you!

1 Like

You could take it a bit further and only check for distance if there’s a target. This way this script only runs when we are outside the range.


Another way to improve the above is to add a test to see if we are already in range when clicking a target - perhaps there’s no need to even start a timer and check for distance.

2 Likes

I just updated it to your new graph and it works great. It also ‘attacks’ when in range.

I had a script before that attackx immediately when in range, so maybe I’ll connect that one now so that I don’t need to start a timer if in range. Thank you once more

1 Like