I’ve been wondering which way is the best to accurately calculate the damage an explosion supposed to deal while still being manageable to handle for a multiplayer game. By “accurately” I mean Line of Sight check as well as damage based on how much area of the player’s colliders is exposed to the explosion. I’ve tried the following things so far:
Overlapsphere dealing damage to every “Player” component on a collider inside the radius. This was just a simple test to get to grips with the problem, as it obviously doesn’t fulfill my criteria of being “accurate”
Find all colliders within Overlapshpere, shoot a raycast to each collider to check line of sight. As my player model is fairly complex (a human consisting of ~20 colliders) some colliders are hidden by others, meaning raycasts may hit the same collider twice. I simply changed it so that if a hit collider has the same root transform as the actual target collider (i.e. the same player) it still counts as a hit. The main problem however is that especially for larger colliders, a single raycast to the collider’s gameobject’s position is very accurate. For example, the chest is a single box collider, if the players chest is hidden by more than 50%, so the center of the collider is hidden, the raycast won’t hit, despite a decent amount of the chest’s area being exposed to the explosion.
Basically, a lot of raycasts. At first, all possibly hit players are stored in a dictionary by checking for colliders with a overlapshere and adding the Player ID (the name of the player gameobject which is individual) string as label to said dictionary, with a float as value (default 0). I have settled on 200 raycasts being drawn spherically using a for-loop with vectors from a pre-calculated vector array. Similar to the shrapnel of an actual explosion. For each raycast hit, the corresponding collider’s root transform’s name is being compared to the dictionary and then the float value is being increased by the damage of a single hit, let’s say +5. If the explosion is going off in front of a player, a lot of raycasts will hit the player’s colliders and the value in the dictionary will rise. After the for-loop I deal the corresponding damage to each player in the dictionary. If a player was further away or partially in cover relative to the explosion, he will have been hit by fewer raycasts and therefore less damage will be dealt.
System 3 works as far as I can tell the way I want it to, however I feel like it might by quite resource demanding to draw this many raycasts and I wonder if there may be a more elegant system to achieve my desired goals.
Thanks for reading and any help appreciated.