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

Hi, I am making a strategy game and there are soldiers in a trench. I want them to be able to throw a grenade (I implemented this), but I don’t want them to be able to throw it too close to their trench.

I thought about measuring the distance between the thrower and the target. The problem is the trench is wide, so if an enemy is to the other side of the trench the distance will be far enough to throw, even though he is too close to the trench.

Basically, I want to draw a line in the map, or define zones in the map, that are flagged somehow. And the add in my grenade throw logic, a branch where I check if the targeted part of the map is flagged or not.

How do i implement this?

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

I used the collision box option and it works perfectly and was easy to implement. Thanks a lot for a great answer!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.