Hi guys! I'm working on a 3D grid-based tactical turn-based game similar to old X-Coms and I've stumbled upon a problem that I'm not sure I know how to solve from a conceptual perspective.
Let's say I have a couple of units from two factions.
Each unit can fire a gun to the enemy units. Bullet fires from the muzzle of the gun and damages the first object it hits. Due to the accuracy of the unit, a bullet can hit not only the targeted unit but virtually any object and other unit (even friendly firing player's other units).
To make an AI controlling some of the units know what cells they can shoot what enemies from, I want to implement a Line of Fire mechanic. Line of Fire is also needed to show the player, where and what units he can shoot with his units.
The simplest approach to knowing if the unit can be targeted from the specified position by the specified unit is to use ray cast from the muzzle of the gun to the body parts of the target unit.
In the old X-Com games, this would perfectly work because all the sprites were static (and overall in the 2D pixelated mess you could never really say, what part of the body you've hit anyway)
But in my case, the problem is that both units are being animated, and their body parts and muzzles are constantly moving due to their animations. and if I'm getting the current position of body parts/muzzle, it will lead to inconsistent results, e.g. one time unit will have LoF to the enemy, and another time, in the same conditions, he will not have LoF due to the unit's idle animation, which moved muzzle to the side.
I thought about possible solutions for this problem:
1. use a fixed virtual point for the gun muzzle, make all ray casts with it, and then (I think that it could be very difficult) try to adjust the real animation so that it will be in line with the virtual muzzle point.
2. precalculate where the muzzle will be when the fire animation will play. Seems almost impossible for me, as I don't know how to handle animation blending and other stuff in this situation.
Both approaches seem valid to me, but yet I have a feeling that other similar games would solve this problem differently.
How would you solve this problem?