# How do I calculate line-of-sight distance between objects

Get help using Construct 2

### » Sat Mar 08, 2014 11:16 am

Hello again,

I need to calculate the line-of-sight distance between two objects (I think).

Background

I currently have Enemy tanks only returning fire once fired upon by a friendly tank. I now want to add some extra Enemy AI which will allow an Enemy tank to detect any Friendly within it's shooting range, who is also shooting, and then engage.

See image below;

In this example there are two Enemy (red) tanks within range (for simplicity I've highlighted the range of the Friendly tank - but in reality this is Enemy centric). What I want to accomplish is these two tanks detecting that the Friendly in their range is shooting, and to begin engaging it.

My thought for achieving this is;
For each enemy unit that isn't firing
• find closest friendly unit
• Calc distance to unit (line-of-sight)
• if distance <= range AND friendly is firing then ATTACK!!

This only gets me the one closest Friendly to the enemy - there could be more friendly in range attacking, but since i can only attack one at a time, this is good enough.

Problem
How do I get that distance figure, and how do I ensure it is line-of-sight and not including a random path route distance?

Any thought appreciated.
B
5
S
2
Posts: 34
Reputation: 514

### » Sat Mar 08, 2014 3:14 pm

I don't think you need to calculate the distance anymore when you use line of sight. At least if you don't want them to be closer together. You could even take the turret target UID and give that to the others.

If you still need the distance you could compare distance between enemy base and enemybases.family.
(distance(Enemy1Base.x , Enemy1Base.y , EnemyBases.x , EnemyBases.y))
B
34
S
9
G
3
Posts: 519
Reputation: 5,300

### » Sat Mar 08, 2014 9:42 pm

Katala, thanks for the reply. Is line of sight a built in function? Is this a Turret characteristic?
B
5
S
2
Posts: 34
Reputation: 514

### » Sat Mar 08, 2014 9:56 pm

Oh sorry I miss understood. I ment that different line of sight behavior. Turret also has range so I think the difference really is that if you use line of sight you can block it by solid behavior.
B
34
S
9
G
3
Posts: 519
Reputation: 5,300

### » Sun Mar 09, 2014 2:13 am

Thanks again Katala.

I wasn't aware of the line of sight behaviour. Enabled it for my turret family and set the range. One simple event and I have the exact behaviour I was looking for, thanks.

B
5
S
2
Posts: 34
Reputation: 514

### » Sun Mar 09, 2014 8:28 am

Thanks!
Game studio with a portfolio of 4 Apple Featured games
B
32
S
7
G
2
Posts: 115
Reputation: 2,465