- Home /
Multiple projectiles hitting the same target at once for shotguns
Basically, I am working on a FPS game with sort of physical projectiles. The projectiles have a component attached containing the values for damage, velocity etc. and the "Player" component of the player who fired the projectile (I call that the "owner" of the projectile). The amount of damage the projectile has done is sent back to the owner and is then displayed in their UI (similar to Battlefield) to keep track of the players score. Let's say I have a gun which deals 30 damage, the number in the UI will go from 30 to 60 to 90 to 100. I achieve that the number doesn't exceed 100 using the code below. This works perfectly fine for normal weapons which fire one projectile at a time, however, with a shotgun which fires multiple projectiles at once (I use a for-loop spawning one projectile each time for that) I run into the problem that the projectiles hit almost simultaneously and the math doesn't seem to be able to keep up. Essentially, every projectile "thinks" the target is still at full health and therefore sends back it's full potential damage to the owner, leading to the owner of the projectiles earning considerably more than 100 score with a single shot.
I have some possible solutions in mind, one which I have tried so far is giving each projectile a random delay by calling CmdPlayerHit with a coroutine with a random yield time between 0 and 0.2s, which obviously also means that the damage is delayed which isn't really great for a FPS. An alternative I coul think of would be to store the amount of damage a projectile has done in a variable on the owner, add them all together and clamp the combined damage so it doesn't exceed the damage when the player was hit by the first projectile, however, I have no idea when I would then "apply" this score as some projectiles might hit a different target at a different time.
If someone has an idea, a comment or a similar problem feel free to answer. Any help appreciated.
The function below is called when the projectile has hit a player.
[Command]
void CmdPlayerHit (string _playerID, float _damage)
{
Debug.Log(_playerID + " has been hit by " + ownerID);
Player _player = GameManager.GetPlayer(_playerID); //gets the "Player" component of the target
float previousHealth = _player.GetCurrentHealth(); //returns the current health of the shot player
_player.RpcTakeDamage(_damage); //makes the shot player take damage
float score = Mathf.Max(Mathf.Min(_damage, previousHealth), 0f); //change the damage to a appropriate number
owner.RpcAddRecentDamagePoints(score); //give the owner the score
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Manually creating "nested" gameObjects 1 Answer
Setting a Target for projectile. 0 Answers
How do I calculate this voxel script so I don't get a out of range error? 2 Answers