- Home /
How to auto attack like in lol or other moba games?
So I am making a small moba game. I have made a click to move system but I don't know how I can make auto attack efficiently with: if in range: attack, if not move closer and then attack, cancel attacking by moving.
I know I can make a collider but is there a better solution? I also want the attack to make damage only on the selected enemy not to everyone it collides with. Thank you.
Answer by Cuttlas-U · Oct 22, 2017 at 04:36 PM
hi; u can do this many different ways; u can do it with colliders or ray cast or just simple click ;
but I will explain u about the collider way ;
1 - u need to learn trigger system ( if u dont know )
2 - a sphere collider ( make the size as much u like and mention this will be the range that player detect enemy ) and attach it to player and check istrigger option; we call this collider "Detector" cause its gonna detect if enemy has trigger with it or not;
3 - add a collider to the enemy too ; so if enemy comes inside that Detector the player can underestand;
3 - create an script and give it to the player ; inside in this script create a trigger function to check if the enemy is in range and has trigger with collider do something;
4 - now create another script for enemy and create a bool variable inside it like this :
public book ImInRangofPlayer;
so this variable has false value by default but we make it true when it has trigger with that detector collider from the player;
5 - inside player script that u created in the trigger function make that variable to "true" for every enemy that come inside the range of Detector;
6- now u need to shoot towards the enemy u can do it many ways but i think u should first do these steps then ill help u with the rest ... ;
good luck ;
Thank you @Cuttlas-U I did what you said and it works. What do I do now?
You should have commented on his answer ins$$anonymous$$d. Just FYI.
Your answer
![](https://koobas.hobune.stream/wayback/20220612141615im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Best way to attack 2 Answers
Random Material 0 Answers
Is this a good way to handle range? 1 Answer