Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Aiming down sights in an FPS?

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.

1 comment

2

You gotta try it out yourself bud, if it doesn't work out then you can come here ask what gone wrong

1 answer

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)

3 comments

0

^ Read this, it will help (has aiming similar to yours)

0

i have already read that by egomoose. i infact did take some code, but it is still being buggy.

This discussion is preserved as it was. New answers, comments, and votes are closed.