How to make minimal firing range for units in a game?

If your map is made of squares, you could build trenches taking a square of space on a map. Similar to a chess board you then can compare square coordinate of the soldier to square coordinate of the trench (say X3, Y7 is a place on the map marked trench.). In this case I’d have a spawner / subsystem keeping track of what is spawned and where, mapped to those coordinates on a property.

You could also add overlapping collision box to the trench actor, so that when soldiers walk into the invisible box (overlap) they are notified that they can not throw a grenade.

Other option is to measure distance between two locations. You could set Z on the location vectors to 0 to measure only horizontal distance:

const FVector LocationSub = FVector(133.f, 155.f, 20.f) - FVector(33.f, 75.f, 10.f);
const float Distance = LocationSub.Length();
1 Like