The image above shows a boxcast, for what im trying to achieve this wont work. Essentially what would be best is to cast a half sphere or a a quarter sphere like I drew on top of the boxcast screenshot.
Im trying to make a system for the cannons to aim at enemies and shoot them, but the cannons on the right side of the ship should never try to target enemies on the left side of the ship, hence i figured this is a reasonable approach: boxcast a single side and go from there.
Alas, the boxcast is extremely limited for this problem as enemies can easily be outside the box, but still on the right side of the ship.
Is there a better way to do this? The boxcast is also limited in that you can only set 2 dimensions, its cubic size and then its length.
I dont think I can use a SphereCast because that would also target enemies of the left side.
Or is there a way to determine which enemies are in front, which are on the right, which are behind and which are to the left, and put those in Lists (rightEnemiesList, frontEnemiesList, leftEnemiesList, backEnemisList
)?
How do i solve this? I dont want to be using 50 line raycasts on each side either, for performance reasons. Not to mention im actually calculating 4 sides, so we have cannons on each side but also on the front and back of the ship to shoot forwards and backwards.
The code for the boxcast, although it doesnt add much to this question:
var half = ship.mainGunCaliber.range * .5f;
var rotationY = hullParent.localEulerAngles;
rotationY = new Vector3(0, rotationY.y, 0);
Quaternion boxRot = Quaternion.Euler(rotationY);
RaycastHit[] hits = Physics.BoxCastAll(hullParent.transform.position + hullParent.transform.right,
Vector3.one * ship.length,
hullParent.transform.right,
boxRot,
half);
Thank you!