How to prevent gun from being able to shoot through walls

So what im trying to do is make it so that the gun is no collisions, but doesnt do as it does in the video, i tried making it collide but you would glitch out and launch and it was still possible to shoot through, how can i fix this? Would raycasting to see if the players head has no obstroctions between it and the muzzle work? (Also the bullet shoots from the muzzle and id like to keep it that way

video:

Cast a ray from the players torso towards the mouse position and see if a ray hits anything except the players character which you should filter out, then if the ray does then simply don’t shoot or shoot and the point of intersection.

how would i detect if the ray doesnt reach the mouse?

I… just told you?

From the player’s torso, send a ray towards the mouse position that is where you want your bullet to hit, then see if the ray hit or not.

local Origin = Character.Torso.Position
local Direction = Mouse.Hit.Position - Origin

local ray = workspace:RayCast(Origin,Direction,Params?)

if ray then return end
1 Like

took some tinkering and some changes but it works, i just casted a ray from the torso to the muzzle and if it doesnt meet or there is a part blocking it (Like you showed) it will just shoot the part it hits instead of through the wall. thanks for your help!

No problem! Let me know if you need help with anything else! :heart:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.