I created a new method for calculating hit/miss shots for shooter type maps a while back. Simply, I compare the players camera angle with the angle between two points. The two points being the player controlled unit and the point of the potential target unit. If these angles are approximately the same, you are aiming in the direction of the target unit.
I wrote my method as galaxy script code, but I will walk you through the concepts.
First, I have a region which is always centered on my player ControlledUnitt.
When units enter the region, they are added to a player group of Targets.
When they leave or die, they are removed.
Next, when the player shoots, my "AngleMatch" function cycles through all of the units in this region that have been added to the group Targets.
It checks compares the CameraYaw with the value of the angle between two points (position of ControlledUnit, position of TargetUnit). If they match, just verify height.
That is the jist of the concept.
Advantages: Much faster execution than traceline.
Disadvantages: Collision detection : if no other methods are added, you can simply shoot through walls.
had to check each point between my target with increments and check the height of the ground and see if it blocks the path of the bullet. So that a hill in front of me I can't shoot thur it. Yours seems like you can shoot thur hills. Then I used regions to check if the point is within region and whether path of the bullet is higher or lower of the custom value i placed for that region.
Rollback Post to RevisionRollBack
Star Shooter Defense
If you want it to go EVEN faster, don't use the distance between points function. That uses square roots, but for this problem, there is no need. Since you are COMPARING distances, rather than printing them out, you can simply compare a^2 vs b^2 rather than a vs. b. This saves you from using a costly square root operation. Just make your own squareDist function that returns the distance squared.
I need the distance to scale my angle. For example, the valid angle bounds is wide when the points are close together, and slim when far apart. I use the distance to calculate the valid angle bounds using trig...if I can do this with squaredDist lmk