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

Aiming down sights in an FPS?

Asked by 3 years ago
Edited 3 years ago

I am making an FPS but i don't know how to allow for aiming down sights. The current code i have might help

local camera = workspace.CurrentCamera
local RS = game:GetService("RunService")
local voffset = CFrame.new(0,-1,0)

--// Weapon Varibles \\--
local equipped = nil
local root = nil
local viewport = nil
local module = nil
local previousEquipped = nil

local function setweapon(weaponname)
    if viewport ~= nil and root ~= nil then
        viewport.Parent = root.Model
    end
    equipped = weaponname
    root = game.ReplicatedStorage.Guns:WaitForChild(equipped)
    viewport = root.Model:FindFirstChildWhichIsA("Model")
    module = require(root.Module:FindFirstChildWhichIsA("ModuleScript"))
    viewport.Parent = workspace
end


setweapon("Glock")
previousEquipped = equipped
RS.RenderStepped:Connect(function()
    viewport:SetPrimaryPartCFrame(camera.CFrame * voffset)
    if not previousEquipped == equipped then
        setweapon(equipped)
    end
end)

game:GetService("UserInputService").InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Two then
        voffset:Lerp()
    end
end)

Can anyone help me?

Edit: Look in the first answer. that is where i will put updates.

2
You gotta try it out yourself bud, if it doesn't work out then you can come here ask what gone wrong User#32819 0 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Just tried to make the sight work, but no luck.

game:GetService("UserInputService").InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        local dist = viewport.Sight.CFrame - viewport.HumanoidRootPart.CFrame
        for i=0.1,1,0.1 do
            voffset:Lerp(dist, i)
        end
    end
end)

Edit: Working in studio right now. Got a new is aiming system

game:GetService("UserInputService").InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        isAiming = true
    end
end)

game:GetService("UserInputService").InputEnded(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        isAiming = false
    end
end)

Edit: Okay, know I'm really confused. I'm trying to get the distance between the PrimaryPart of the viewport model, and then I offset the viewport model by the distance. But it ends up rotating everything!

RS.RenderStepped:Connect(function()
    viewport:SetPrimaryPartCFrame(camera.CFrame * voffset)
    if isAiming then
        local offset = viewport.HumanoidRootPart.CFrame:toObjectSpace(viewport.Sight.CFrame):Inverse()
        voffset = offset
    else
        voffset = vOffsetDefault
    end
end)
0
^ Read this, it will help (has aiming similar to yours) WoTrox 345 — 3y
0
i have already read that by egomoose. i infact did take some code, but it is still being buggy. busterk66 18 — 3y
Ad

Answer this question