Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I check if my gun is colliding through a wall?

Asked by 7 years ago
Edited 7 years ago

I'm currently making an fps and I am stumped on how to proceed with preventing shooting through walls (sticking your gun through a wall then firing).

Currently I'm considering giving the gun a hitbox and preventing it from firing if the hitbox hits a wall (edit: INSIDE a wall), however, I have no idea how to do this - I've experimented with touched and I don't know how to properly use it to do this. Any help is greatly appreciated! Thanks.

0
You could try casting a ray from the tip of the gun to your player's head, if it can't hit, it would mean they're shooting through a wall Perci1 4988 — 7y
0
I don't mean bullet collision detection, I mean actual weapon detection - the weapon itself has a hitbox and can't fire if it is stuck through a wall to prevent shooting through walls, where the tip would be on the other side of a wall. coolyoshipower 42 — 7y
1
yeah that's what i'm saying, you could try casting a a ray from the tip of the gun to your head. if there's something in the way, they're probably trying to shoot through a wall. Perci1 4988 — 7y
0
Ah, I see. Sorry, I misread your previous answer. I'll try this now. coolyoshipower 42 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

I was afraid of someone doing this while making my gun for a murder game,so i decided to make a ray that comes from the head of the player,instead of the gun.

--Local Script

tool=script.Parent
clickEvent=tool.ClickEvent

tool.Equipped:connect(function(mouse)
    mouse.Button1Down:connect(function()
        clickEvent:FireServer(mouse.Hit.p)
    end)
end)

--Server script

tool=script.Parent
clickEvent=tool.ClickEvent

tool.Equipped:connect(function()
    local player=tool.Parent
    clickConnection=clickEvent.OnServerEvent:connect(function(hit)
        local ray=Ray.new(player.Head,hit)
        local part=workspace:FindPartOnRay(ray,player)
        if part.Parent:FindFirstChild('Humanoid') then
            part.Parent.Humanoid:TakeDamage(30)
        end
    end)
end)


Hope this helped ya :D

Ad

Answer this question