So lately I have been wondering what is the best way to create a hit verification for a gun, let’s break this into two parts:
Part1: How to detect a bullet hit?
Despite all the server/client relationship, I kinda want to know how to I create a bullet hit. We use raycasting to cast the route for the bullet. And for me, I use TweenService to tween a BulletPart along the raycasted route. The bullet needs time to travel, and it might take less than a second before it even hit a player’s character. According to the Wiki page Tutorial on how to make a laser gun, the variable part is being used to find the raycast’s terminal. However, if a bullet speed is being implied, the variable part will be the raycast’s terminal before the tween even finishes. For an example, a player moved when the weapon that was aiming at the player was fired, the variable part will be the player even though the player has moved away when the bullet arrives. For instance, with this raycasitng, player could not doge bulletes.
I tried to use TouchEvent for the bullet instead of using the raycasting variable, however I did not find this being consistent. Sometimes the TouchEvent did not even fire. For an example, a shotgun shoots multiple bullets, while the bullets were shot, they collide with each other and the TouchEvent will registered to the bullets. And they did not fire again when they touch the player, probably due to the bullet travel time is too short, sometimes even less than half a second.
Part2: How about client/server relationships?
How does shooter games like Phantom Forces, Arsenal did such job? There will be a sight delay if the shooting function is on the server, did they replicate the bullets to the server and check whether they hit the player or not? (Which leads back to Part1) or there is other such way?
Please let me know if you have any confusion, I tried my best to explain the case!
Raycast on the client, confirm the hit and apply damage on the server for your second question. (To not show delay to the shooter, though the server will still delay but the target wont know that)
I always thought that you are supposed to raycast as you move the bullet instead of tweening the bullet to a predetermined destination. This would guarantee that the bullet moves dynamically with the environment.
I think the way phantom forces does it is they don’t even render a bullet part unless its your own or nearby, and even then they use some kind of weird trail gui effect.
If I were to architect the infrastucture, I would use a remote event that takes a direction (or something like that), enough data to replicate the bullet from the server, adds effects to the bullet for drop and spread, and the server does this too, but doesn’t replicate the movement from the client, rather creates its own bullet object and adds this drop/spread on its own. The spread could probably be made the same by setting the random seed to the same number as the client. Then, they would be using this object as a model for raycasting, and use the results from workspace:FindPartOnRay to see if the bullet hit anything, instead of using an actual part and an unreliable .Touched event, especially since the bullet is travelling at high speeds.
What I do for my guns is raycast forwards from the barrel and check: If the ray hits a Part that belongs to a Model that has a Humanoid, damage is applied to that Humanoid.
The gun is aimed by moving rotation the body and head at their joints.
When the gun is fired, a local script fires a Remote Event that then activates the script that actually raycast.
I use flash for gun effects, but you are still able to pair it with TweenService.
If you want bullet physics like bullet drop and stuff like that, search up FastCast on the forum. The FastCast thing is really neat for shooter games that need accurate physics but without the mess.