Enemy AI for Range of Fire and Line of Sight - Top Down player

Favourite 47 favourites
Tutorial written by krishOriginally published on 16th, November 2012 - 2 revisions

C2 Turret Behavior Supersedes this

When I wrote this tutorial, there was no turret behavior in C2. It exists now, so may be the functionality of the behavior is much better than this. Try turret behavior. :)

Please Note:

I didn't make this tutorial too detailed on assumption that you are well acquainted with scirra basics like spawning an object or picking instances. Wherever, you face difficulty understanding, please search the same line on Scirra forum and you should be able to get how it is done.

Apology for that, not much time on hands to do a full detailed tutorial.
Just spent a freaking full caffeine filled night to get this to work.


Hey all,

I've been writing this top down tank game prototype and one of the major issues I faced is to let the enemy units decide when to start firing.

So this tutorial is to share how it was achieved.

State of Game before reading further:

1. You have enemy units placed and your window size is smaller than your Layout size.
2. You have let enemy turn to player position and shoot every 1 sec or 5 secs.


1. Enemy units keep shooting at you from all directions whether you see them within your window size or not.
2. Enemy units keep spawning un-necessary bullets that hit some solid in between you and enemy if you are not in line of sight.

What was tried, but didn't work according to expectations:

I tried this Line of sight example,
- but to tie a UID to every line of sight seemed restrictive
- it doesnt address the range of enemy fire issue
- didn't work properly with multiple instances of same enemy types
- the for each loop consumes more computing resources specially if you dont want all enemies to fire at once (especially if there are five enemies who have line of sight.. you're fscked :D )

But that example did help a bit in part of how I formed the solution.
Thanks @dcrew for that :)

What are we going to achieve:

1. Enemies should have a range / radius within with if player closes in, they decide to shoot
2. Enemies should not un-necessarily shoot if the player is behind a wall or tree


Duke 3,365 rep

Thank you so much! And thank you for the note: when I started using construct I wasn't able to do almost nothing, but thanks to people like you now I can do my little games! I hope to be able to write a tutorial soon!

Cheers, man!

Friday, November 16, 2012 at 3:23:35 PM
Jander 3,374 rep

Awesome tutorial!

Friday, November 16, 2012 at 4:55:49 PM
krish 6,292 rep

@Duke, @Jander

I am glad you liked it :)
Thank you.

Sunday, November 18, 2012 at 4:01:37 AM
armed10 4,248 rep

I can't get every enemy who's in range to shoot. How would I go about tackling this?

Saturday, March 23, 2013 at 5:56:18 PM
Lordshiva1948 44.3k rep

I started using construct I wasn't able to do almost nothing, but thanks to people like you now I can do my little games! I wish you have some example so I can download and look at it properly. Thanks

Wednesday, July 24, 2013 at 10:27:53 AM
thehorseman007 14.8k rep

Good tutorial , thanks a lot. Very handy.

Sunday, September 08, 2013 at 8:12:14 PM
ferguson4848 951 rep

Well done, very clever. I've implemented this into my enemies that also have a Find Path behavior. Only one enemy can shoot at a time, but that is better than them all shooting at the same time.


Friday, February 07, 2014 at 4:43:45 PM
beardedeagle 2,020 rep

I am dealing with a game where this will come in handy. Thank you!

Thursday, July 03, 2014 at 8:05:48 PM
KoolEcky 16.3k rep

Thanks for your efforts. You make our community richer!

Thursday, September 29, 2016 at 12:49:42 PM
DylanHuber 1,103 rep

what is The maximum distance that line-of-sight can reach?

Tuesday, December 20, 2016 at 8:38:55 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.