# Sight'n'Shoot

0 favourites
• 18 posts
• Hey Friends <img src="smileys/smiley1.gif" border="0" align="middle" />

someone has an idea to get an enemy to shoot only when he "see's" the player?

Greets, xeed

• how are you defining 'seeing'

is it a line of site issue, or is the enemy supposed to be in certain range of the player (like in a radar)?

• a range of vision? like metal gear solid?

platform game or 8 direccion game?

• Yes, there is several ways to do that ... you can create a invisible sprite as the "enemy view" and if this sprite collide with the player, the player is seen and so shoot ... you can also shoot invisible "enemy view" bolts from the enemy toward the direction seen and if collide with player the player is seen etc ... try to draw a invisible line between enemy angle of view and the player ... or more simple just test the range abs(enemy.x-player.x) < view_range and abs(enemy.y-player.y) < view_range

• just some solid objects between the enemy and the player. if there can be drawn a "line" between both, he is seen...and the enemy will shoot ;)

platform or 8-direction doesn't matter.

naelian: thanks, the bullet test should work. i just thought of a behaviour available which solves that without additional objects.

thanks all :)

• EDIT: This method is unnecessary. Go a few posts down.

With this kind of thing, it is very easy to slow down your game. Make sure you aren't making too many checks per tick.

Just make the enemy spawn a bullet towards the player then use a loop to move until it hits an obstacle. If it hit the player, he can see him, if not, then not.

I wasn't sure if you had solved it or not, but there's my method anyway.

• A little math will do it for you without behaviors or additional objects, in a way that's easier to tweak, and in a way that's overall much simpler. This would be the best method in my opinion. First, establish how far the enemy can see in front of him (SeeingDistance) and how big his field of view is (FieldOfView). Imagine a viewing cone.

Now, you just need to make a condition that tests these 3 things:

distance(Enemy.X,Enemy.Y,Player.X,Player.Y) <= Enemy.SeeingDistance

angle(Enemy.X,Enemy.Y,Player.X,Player.Y) <= Enemy.Angle + FieldOfView/2

angle(Enemy.X,Enemy.Y,Player.X,Player.Y) >= Enemy.Angle - FieldOfView/2

The distance will tell you whether or not the player is close enough to potentially be seen. The angle will give you the angle between the player and the enemy. You have to check whether the enemy is turned to that angle, but with a little leeway, given by the field of view. We add half the FieldOfView to either side of the angle to get a viewing cone centered around the direction the enemy is facing.

• Dalal, that is all well and good however if you want the view to be blocked by objects, then it is impractical to use a purely logical method - it will be much simpler to use the bullet method.

EDIT: I just thought of an idea which would be better... Why not just extend a line (make a stretched sprite) from the enemy to the player? Then you can just test if it is overlapping an obstacle, so he cannot then see the player. This is superior to the bullet test as it only requires 1 collision test per tick.

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• sqiddster, you are completely right. I didn't read xeed's last post properly about being able to draw the line.

• No problem... your method is obviously better for a view cone function ;)

• Math lovers are here, your enemies are all robots !

I aggree with sqidders about the draw line/strech sprite method (I have speak about that in my first post men !) ... but i love the bullet method as bullets can take some time to move and so it's more easy for the player to hide before be seen ... the more time the enemy stay in line of view with player the more far the "view bullets" are fired ... this "view bullets" objects must be destroyed if enemy view change and can also be destroyed using a fade in time destroy behaviour to avoid using too many cpu. But don't say it take's always a lot of cpu !!! ... shooting invisible bullets or shooting "real" bullets it's near the same for the game ... same objects ... some are invisible others not ... so if your game can not handle this, it can not handle many enemies shooting at the same time so it's a good way to know if your game will be playable or not !

• are you suggesting bullets in real time? That wouldn't give real line of sight at all... Why not just manually integrate a delay into the other method?

• Yes i'm suggesting firing some invisible bullets every X from the enemy toward the player ... not something really different than drawing a line but a bullet "dash" line with speed effect and so less effectiveness :

In a game such as Splinter Cell some elements such as darkness, or player stealth mode are important to make a difference between "in line of sight" and "seen", it's not just a simple delay. By using a bullet mode you can adjust speed of bullets, bullets size and life time according to darkness and reduce player "shape" according to stealth mode. By doing this you give more "life" to the line of sight system and integrate a failure, it's not 100% effective such as humans behaviour !

How many times have you search something which was just in front of you ?

• thanks for the discussion.

didnt find a working solution yet. i want the enemies also to view at the player only (angle) when there is no object of a specific family (BlockedObjects) between them. that makes it a bit harder to create the event group "sightCheck".

currently, i spawn invisible bullets for each enemy every 0.1 seconds, and set the angle of the bullet manually towards the player.

then i set the angle of the enemy towards the player, only when he is hit by the invisible bullets spawned by the specific enemy.

anyway, this does only work when the player is overlapping an enemy. no idea why.

the invisible bullets are destroyed on BlockedObjects and outside screen. i cant recognize a real performance decrease.

do you have an idea how to solute this, maybe a capx?

• sorry for pushing the thread,

the only problem is, how do i "connect" the invisible bullets to a specific enemy, so that only the one turns around to the player?

<img src="http://construct-games.de/wcf/attachments/public/problem.png" border="0" />

this one does not work ..

any help appreciated <img src="smileys/smiley1.gif" border="0" align="middle" />

• 18 posts