Making an Instant Hit-Detection system regardless of ping / latency

Hello!

I have been searching for a solution on how to make a fast and instant hit-detection system for projectiles no matter how high your ping / latency is.

  • In other words, even if your ping is like 100ms - 200ms, your projectile should register the hit immediately after it hits a player

My current system casts bullets, effects, and anything visual on the client while all the technical stuff like calculating damage, checking cooldowns, and detecting hits are on the server.

  • Once the client inputs to fire a projectile, the client will cast a visible projectile
  • The client sends the data to the server as well, and casts a ray on the server.
  • The server deals damage once the ray casted on the server finds something
  • The server sends data back to the client (damage indicators, etc.) if the server ray hits a player

However, I always come to the problem where high latency will make an unresponsive gameplay experience.

Take these two clips from my system:

20ms-30ms

100ms

As you can see in the first clip, feedback is given immediately after the projectile hits the NPC.
The second clip however, feedback is given with much delay compared to the first clip as I am running that session with artificial 100ms delay

All in all, how would I approach a fast yet secure hit-detection system for projectiles regardless of ping / latency?

Any help would be appreciated. Thanks ya

1 Like

One recommendation could be to implement raymarching into your projectile motion (assuming that you haven’t implemented it already), where you essentially divide up the ray cast of the projectile’s hit detection into small steps which should yield efficient results. Hopefully, this should reduce the load on the server for replication and reduce the latency provided that the time between each raymarch is long enough. Here’s a diagram to illustrate this:

In addition, you could use this method to calculate a “prediction” for what object the projectile will most likely hit using a Region3 check in the ray march step’s direction. This would require you to use fewer steps, and you’d be able to account for latency by sending the damage indication data back to the client a few milliseconds before a player is even hit.

It’s all a tradeoff. You can make it so that all players tell the server when they hit. This is obviously not very secure. You could have the server the the true decider firing the ray and checking. This is very secure but as you noted, latency affects it dramatically. You could also have the client report a hit, but have the server verify that the player they hit was recently where the shooting client claimed, and that the projectile would have hit them. This is somewhat secure, but still has a bit of wiggle room for cheaters. You could also have the server do all of the verification, but have the client report when they detected a hit on their end. This means they will get some false positives and false negatives, but it will feel more responsive overall. (I couldn’t see the video so I don’t know the exact context).

This looks promising, appreciate the reply!
I have a tiny concern though

Predicting shots could create some false positives right? For example, what if someone gets in the way right after you shoot a projectile before the server knew.

Like if I shoot at the NPC from a far away distance, im assuming by your method, have a bunch of rays ready to be fired like the green lines in your example → then the rays would fire on the client and server to check if theres anything there → if not, then send damage indication data a little early to account for latency

But someone could get in the way while calculating this right??!?!?

Yeah at the moment I have the server be the true decider of checking damage on the server ray’s end.

I have been considering having the client report a hit and the server verify that hit, but lets say that the player’s bullet hits an NPC and then sets up data to send to the server. The data sent by the client to the server will have some latency so it will still have some delay.

Rn I have two bullets, one on the server and the client that are running in parallel. The server bullet detects all hits while the client’s bullet is all visuals and sorts. Since the server bullet basically starts much later than the client’s bullet, im not sure how to get these guys to sync lol

This would depend on how spaced out your raymarch steps are. You would have to find a balance between having large enough steps so that fewer Region3 checks are required and latency is reduced, and having small enough steps so that more Region3 checks are required which would increase latency but are more accurate. I think playtesting would be the most effective method for determining this balance while trying out various latencies.

On another note, if your Region3 checks are small enough, you can account for another player getting in the way and damage them instead.