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

How to make a gun attached to camera shoot?

Asked by 4 years ago

Hi so recently I've been experimenting with the camera and so I scripted a gun to the camera. I honestly don't know how to make it shoot. Any help would be nice!

The video I watched: https://www.youtube.com/watch?v=T_uMjXoU8YQ

The framework code:

local player = game.Players.LocalPlayer
local cam = workspace.CurrentCamera
local mouse = player:GetMouse()
local rep = game:GetService("ReplicatedStorage")
local gun = rep.Guns.G36C
local run = game:GetService("RunService")

for i,v in pairs(gun:GetChildren()) do
    if v:IsA("BasePart") then
        if v ~= gun.PrimaryPart then
            local weld = Instance.new("Weld")
            weld.Part0 = gun.PrimaryPart
            weld.Part1 = v
            weld.C0 = gun.PrimaryPart.CFrame:inverse()
            weld.C1 = v.CFrame:inverse()
            weld.Name = v.Name
            weld.Parent = gun.PrimaryPart
        end
    end
end

gun.Parent = cam

run.RenderStepped:connect(function()
    gun:SetPrimaryPartCFrame(cam.CFrame*CFrame.new(0,-1.1,.2))
end)


I would like to make it shoot! THANKS!

0
Any help works! Obtrive 0 — 4y

1 answer

Log in to vote
0
Answered by
BashGuy10 384 Moderation Voter
4 years ago
Edited 4 years ago

The best place to make guns is here.

The tutorial linked will help you learn raycasts.

Here is some sample code:

-- Taken from the link above
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Bright red")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false

local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

game:GetService("Debris"):AddItem(beam, 0.1)

The code above, will create a beam towards the mouse position.

Here is another tutorial for guns, with animations!

Hope this helps!

0
If you need any help with actully making it shoot, message me on roblox! BashGuy10 384 — 4y
0
The youtube tutorial linked in the question will show how to make a FE gun. BashGuy10 384 — 4y
0
Hi so this is my main account i sent you a message magicalas1 5 — 4y
0
nvm I can't :( magicalas1 5 — 4y
Ad

Answer this question