Sight'n'Shoot

Get help using Construct 2

Post » Thu May 24, 2012 4:04 pm

Hey Friends

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

Greets, xeed
B
15
S
3
G
5
Posts: 396
Reputation: 4,977

Post » Thu May 24, 2012 4:38 pm


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)?
B
11
S
2
Posts: 20
Reputation: 1,042

Post » Thu May 24, 2012 4:58 pm

a range of vision? like metal gear solid?

platform game or 8 direccion game?
B
17
S
3
G
1
Posts: 28
Reputation: 1,763

Post » Thu May 24, 2012 5:03 pm

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_rangenaelian2012-05-24 17:15:37
B
16
S
5
G
3
Posts: 298
Reputation: 5,240

Post » Thu May 24, 2012 5:56 pm

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 :)
B
15
S
3
G
5
Posts: 396
Reputation: 4,977

Post » Thu May 24, 2012 6:09 pm

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.sqiddster2012-05-24 18:27:00
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,390

Post » Thu May 24, 2012 6:20 pm

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.
Dalal2012-05-24 18:24:02
Image
B
10
S
3
G
2
Posts: 196
Reputation: 2,053

Post » Thu May 24, 2012 6:24 pm

@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.sqiddster2012-05-24 18:26:17
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,390

Post » Thu May 24, 2012 6:28 pm

@sqiddster, you are completely right. I didn't read xeed's last post properly about being able to draw the line.
Image
B
10
S
3
G
2
Posts: 196
Reputation: 2,053

Post » Thu May 24, 2012 6:32 pm

No problem... your method is obviously better for a view cone function ;)
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,390

Next

Return to How do I....?

Who is online

Users browsing this forum: Megavolt, mnashwan, stylie1 and 19 guests