Creating realistic bullet movement

So I decided to start working on gun/projectile based weapon system. I’ve decided Im going for more realism, so simple hit scan wont do.

Thus is why I’ve come here. Obviously doing bullet physics could be quite difficult, along with how it could be quite straining on the server.

So I figured what I could do is run the whole thing on the client, and then with some validation I could confirm most of the things to be close enough to where the bullet would’ve actually traveled, thus stopping anyone from just hitting every single player.
But I wasnt sure if this was the best way to go about this, so I’ve come here for guidance!
How should I go about making a system like this? What are some extra validation checks I can use?
Any other advice or notes?

Thanks in advance!

2 Likes

So for the client you 100% want to handle all the visuals, like bullet tracers and stuff like that. And since you said you want to simulate the physics on the client, you would need to send a remote to the server.

Assuming you want drag and gravity, you could do some math like this

local pos = muzzlePos
local vel = dir * muzzleVelocity
local g = Vector3.new(0, -workspace.Gravity, 0)
local drag = 0.002 -- Whatever value you find best

RunService.Heartbeat:Connect(function(dt)
    -- drag force
    local dragForce = -drag * vel.Magnitude * vel
    -- acceleration
    local acc = g + dragForce
    -- update velocity
    vel = vel + acc * dt
    -- update position
    local newPos = pos + vel * dt
end)

And you can stop the bullet when it hits something by raycasting.

For the server check, you probably won’t need to check if the bullet has the correct position each frame. You just need to make sure if the hit is realistic or could have happened.

You could do an angle check, comparing the client’s aim direction with the actual target. If your target was 100 studs to your right but your aiming straight, then invalidate it.

Of course you also need to check for walls, so you can do a raycast from the muzzle toward the hit position.

In short, you don’t need the server to validate everything, just being close enough is good enough.

1 Like

Sorry for the late response!

Ok so I think I figured out all the physics related stuff out, but now I have one issue, that being moving the actual bullet.

Now in theory I could just do this:

Object.Position += Veloctiy * deltaTime

But depending on someones framerate, and how fast the bullet is moving, it might like skip over certain area.

An idea I had to combat this is just making the hitbox size (cause Im using workspace:GetPartsBoundInBox) as big as the speed, thus it wouldnt be able to skip certain areas, but if I were to do that it might hit something before it even reaches it, which would look really weird.

So do you have any ideas on a work around for this?

Delta time should automatically account for frame rate, as it is the time in between events.

This is true, but again, if the velocity is really fast like more than 50, and the bullet isnt that big, then it most likely wont move as smoothly if you have less then 60 fps, or the speed is like 170 or something.

But I think I have a solution, instead of setting the size of the hitbox to the absolute speed, I could set it the speed * deltatime which would make it smaller, and only cover the area it would be in/go through.

I dont know if that made any sense, but I think I figured it out.

Nice, glad you figured it out. Let me know if you have anymore questions.

1 Like