Prevent bullets going through walls

I’m getting problems where you can shoot through walls. Problem is due to where the attachment is in my view model of the gun, however I am unsure how I can prevent the problem in the first place?? I’m using FastCast btw

local Origin = firePoint -- Attachment.WorldPosition
local Direction = (mousePosition - Origin).Unit

local ActiveCast = Caster:Fire(Origin, Direction, SPEED, CastBehavior) -- Fire shot

Maybe create a small part at the attachment point, and every time you shoot, run get touching parts on it. Check if the parts it touches have can collide on, and if they do, run the ray hit function at the attachment point.

You could cast a ray between the character’s head and the barrel’s end position then disable firing when the ray finds a part. You’d probably want to use a blacklist with the ray that excludes the character and the gun itself.

4 Likes

You could try turning on collision for the gun

won’t that ruin gameplay???
or am I wrong

no not really. It only makes the gun collide able.

Some good solutions have been mentioned here but I figure I’d rattle off a couple others:

  1. Increase the collision bounding box for the player to match the length of the gun. (Not sure how easy or viable this is in Roblox.
  2. Check a region3 infront of the player to see if any part exists that is tagged to block bullets. (Someone mentioned just whitelisting the player/gun and this is okay but there might be some objects you do want to shoot through, like other players, water, glass, or other stuff. You can either whitelist all of them or just tag the objects you dont want bullets to go through to approach the problem from the other end. Whitelist vs Blacklist. Just depends what’s easier.)
  3. Make sure the bullet is bring created on the other side of the wall instead. So just have the bullets created from the player’s chest or so instead of the tip of the gun. You can turn their visibility off until they pass the barrel. (This is probably one of the easier methods. Assuming your player can’t push their body through the wall.
  4. Thicker walls. (ehhh… i mean it works i guess.)

You could add a raycast that casts from the back of the gun to the barrel and checks for obstructions, but that will probably add more latency to any existing gun scripts.

This is my exact problem.

I’ve heard other similar suggestions, like cast a ray from the character head to the barrel and check for obstructions, and frankly I don’t know if raycasting is a performance heavy task - But nonetheless doubling use of raycast for every shot in my opinion would be a bad idea.

Do you know of any industry standard solutions? It’s been almost 3 years since this post.

1 Like