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. :)
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.
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