A lot of games in and outside of roblox has an effect to where if a bullet lands near you, it creates a camera effect as if your character begins to panic. (I.e. The screen gets darker or begins to shake) This effect is particularly used to experience suppression when being under fire.
I’d assume that all these effects and the handling would be on the client. What would be the best way (performance wise) to detect the gunshots that land near you? Loop check the distance between spawned bullet hits and your character?
Definitely not the best but currently if I were to do this, I would create a folder in ReplicatedStorage, for example, HitPoints. And then when a bullet is shot, the server will create a Vector3Value in that folder denoting the position the bullet hit with maybe a life of roughly 10-15 seconds, before they get cleared from Debris,
On the client, you can then do something like so,
local client = game.Players.LocalPlayer
local RS = game:GetService'ReplicatedStorage'
local CLOSE_DISTANCE = 15 -- The distance in studs a hitpoint must be for the player to get this effects
RS.HitPoints.ChildAdded:Connect(function(c)
if client:DistanceFromCharacter(c.Value) < CLOSE_DISTANCE then
-- Handle effect here
end
end)
Again though just to be clear, this is definitely not the best method, this is simply the best method from my own point of view and my current programming abilities, another method you could do is to that when a bullet is fired, when you parent them in the server, put them under a specific BulletFolder and then run that childadded check to that folder and check for the hitpoint you’ve created in that bullet instance
Just for clarification, you are specifically referring to bullets that land near the player, and not all bullets that whizz by the player (e.g. fly past the player but land miles away), correct?
If this is the case, you could check the Position value of where the bullet landed and compare that with the position of the player: either by loop checking (what you said) or have an event fire every time a bullet lands, passing its final Position as a parameter. Not sure how this might impact performance, and how to optimize though. Firing an event for every bullet will probably be costly.
If your looking for all bullet whizzing by, it gets a lot more difficult but if your not working with any bullet drop it could come down to a relatively simple formula, but, once again, I’m not sure what impact that would have on performance and how to optimize.
Thanks for the detailed response! Seems like what I had in mind was the right direction. I’ll probably have to do some tests to see how performance heavy checking the distance in every shot added would be. Especially if it’ll be a game filled with tons of players firing numerous bullets at a time.