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

0 favourites
• 6 posts
From the Asset Store
Welcome! I-Spy (Hidden objects) is an educational puzzle that is more than just seek-and-find activities.
• 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.

• 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))

• Katala, thanks for the reply. Is line of sight a built in function? Is this a Turret characteristic?

• 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.

• 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.

• ## Try Construct 3

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

Construct 3 users don't see these ads
• Thanks!

• 6 posts