How to fix my viewmodel?

My viewmodel is not working very well.
I have video of my issue. I hope someone will really help me.
Video:


And the example that EXACTLY explains what I want:

I have another example:

And the code:

local aimCF = CFrame.new()
local aimOffset = Vector2.new()
local camera = workspace.CurrentCamera
local LastCameraCFrame = camera.CFrame
local uis = game:GetService("UserInputService")
local viewmodel

local equipped = false

script.Parent.Equipped:Connect(function()
	viewmodel = game.ReplicatedStorage.Viewmodel:Clone()
	viewmodel.Parent = camera
	equipped = true
end)

script.Parent.Unequipped:Connect(function()
	viewmodel:Destroy()
	equipped = false
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if equipped then
		local mousedelta = uis:GetMouseDelta()

		--if camera.CFrame == LastCameraCFrame then
		aimOffset = aimOffset + Vector2.new(mousedelta.X,mousedelta.Y)
		--end

		local rot = camera.CFrame:ToObjectSpace(LastCameraCFrame)
		local x,y,z = rot:ToOrientation()
		LastCameraCFrame = camera.CFrame

		local swayCF = CFrame.Angles(math.rad(math.clamp(-aimOffset.Y/8, -15, 15)),math.rad(math.clamp(-aimOffset.X/10, -15, 15)),0)
		
		local offset = CFrame.new()
		
		aimCF = aimCF:Lerp(swayCF, 1)
		
		viewmodel:SetPrimaryPartCFrame(camera.CFrame * aimCF)

		local humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
		if humanoid then
			--local currentTime = tick()
			if game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").MoveDirection.Magnitude > 0 then
				local bobOffset = CFrame.new(0, -humanoid.CameraOffset.Y/3, -humanoid.CameraOffset.Z/3) * CFrame.Angles(0, math.sin(tick() * -4) * -.05, math.cos(tick() * -4) *-.05)

				viewmodel:SetPrimaryPartCFrame(camera.CFrame * aimCF * bobOffset)
					--[[local bobbleX = math.abs(currentTime * 10) * .25
					local bobbleY = math.abs(math.sin(currentTime * 5))   
					
					local bobble = Vector3.new(bobbleX, bobbleY, 0)
					
					cf = cf:Lerp(CFrame.Angles(0, bobbleY, 0), .1)]]
			end
		end
	end
end)

hi! aim offset is accumulating too much, clamp it

Ok I will try your variant. I will text if something will go wrong

1 Like

I already clamped It. Do you have any other ideas?

this is how i did it one game

local mouse_delta = UserInputService:GetMouseDelta()
local sensitivity_scale = 60 * fps_to_delta
local scaled_mouse_delta_x = mouse_delta.X / sensitivity_scale

weapon_tilt_spring.Target = math.clamp(-scaled_mouse_delta_x, -0.5, 0.5)
local current_weapon_tilt = weapon_tilt_spring.Position

tilt_spring.Target = math.clamp(tilt_spring.Target - (mouse_delta.X * 0.3), -viewmodel_tilt_angle_max, viewmodel_tilt_angle_max)
local current_tilt_offset = tilt_spring.Position

I do not really know how to use your code because I don’t use spring module

try this and lmk how it goes

local aimCF = CFrame.new()
local aimOffset = Vector2.new()
local camera = workspace.CurrentCamera
local LastCameraCFrame = camera.CFrame
local uis = game:GetService("UserInputService")
local viewmodel

local equipped = false

script.Parent.Equipped:Connect(function()
	viewmodel = game.ReplicatedStorage.Viewmodel:Clone()
	viewmodel.Parent = camera
	equipped = true
end)

script.Parent.Unequipped:Connect(function()
	viewmodel:Destroy()
	equipped = false
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if equipped then
		local mousedelta = uis:GetMouseDelta()

		aimOffset = aimOffset + Vector2.new(mousedelta.X, mousedelta.Y)
		aimOffset = Vector2.new(
			math.clamp(aimOffset.X, -30, 30), 
			math.clamp(aimOffset.Y, -20, 20)
		)

		local rot = camera.CFrame:ToObjectSpace(LastCameraCFrame)
		local x, y, z = rot:ToOrientation()
		LastCameraCFrame = camera.CFrame

		aimOffset = aimOffset - Vector2.new(math.deg(y), math.deg(x))

		aimOffset = Vector2.new(
			math.clamp(aimOffset.X, -30, 30), 
			math.clamp(aimOffset.Y, -20, 20)
		)

		local swayCF = CFrame.Angles(math.rad(math.clamp(-aimOffset.Y/8, -15, 15)),math.rad(math.clamp(-aimOffset.X/10, -15, 15)),0)

		local offset = CFrame.new()

		aimCF = aimCF:Lerp(swayCF, 1)

		viewmodel:SetPrimaryPartCFrame(camera.CFrame * aimCF)

		local humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
		if humanoid then
			if game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").MoveDirection.Magnitude > 0 then
				local bobOffset = CFrame.new(0, -humanoid.CameraOffset.Y/3, -humanoid.CameraOffset.Z/3) * CFrame.Angles(0, math.sin(tick() * -4) * -.05, math.cos(tick() * -4) *-.05)

				viewmodel:SetPrimaryPartCFrame(camera.CFrame * aimCF * bobOffset)
			end
		end
	end
end)

Okay, now It works properly! Thank you so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.