How do I make my FPS gun aim down sights?

I am assuming you have the viewmodel humanoidroot part set to the camera CFrame or something similar.

	local goalCFrame = camera.CFrame
	psuedoHRP.CFrame = goalCFrame

You can obtain a CFrame that goes from this HRP → sightCFrame

local STATIC_OFFSET = psuedoHRP.CFrame:ToObjectSpace(aimAttach.WorldCFrame)

If you notice this is also pretty similar to EgoMooses tutorial which I learnt from as well. I also recommend checking it out.

However here I chose to use both tween service and lerping. TweenService because it can provide multiple easing styles, lerping is just used to adjust the aiming CFrame offset from 0 to 1.

--outside the loop
local i = 0
local duration = 0.5 --Time it takes to get into aiming position
local STATIC_OFFSET = psuedoHRP.CFrame:ToObjectSpace(aimAttach.WorldCFrame)

--In the renderstep loop
	local goalCFrame = camera.CFrame

	psuedoHRP.CFrame = goalCFrame
	
	if UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton2) then
		i = math.min(i + dt/duration, 1)
		else
		i = math.max(i - dt/duration, 0)
	end
	local addedOffsetToAimAttach = STATIC_OFFSET:Inverse()*CFrame.new(0,-0.1,-3)

	local lerpAlpha = TweenService:GetValue(i, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	psuedoHRP.CFrame *= CFrame.new():Lerp(addedOffsetToAimAttach, lerpAlpha)

This code was taken from my Spring viewmodel post which constructs the view model purely in code for sharing purposes.

This should be a good starting point for reference.

Because as you have noticed the rotation point of the gun when aiming is pretty weird which is because the gun is offset to the right of the view model.

Normally when aiming the gun becomes centered in the middle changing the rotation point through an aim animation.

12 Likes