Trying to get this camera sway script to work with my realistic first person script

Hey guys. I have 2 scripts that arent working together. I think I know the reason but I just dont know how to fix it.

I have a script that makes first person look “realistic”. It also makes the mouse movement smoother in a sense that it delays the mouse movement by a bit. I think this script is overriding my sway camera script leading it to not work. I will provide videos of what both scripts do, then provide the scripts.

Note: In my first person script im also controlling something i have called interactables. Just skip over that.

Realistic First Person:

Camera Sway:

Realistic First Person Script(the part that needed fixing):

RS.RenderStepped:connect(function()
	if _G.booted == false then 
		cam.CFrame = workspace.CamPart.CFrame
		return
	end
	if running then
		updatechar()
		CamPos = CamPos + (TargetCamPos - CamPos) *0.28 
		AngleX = AngleX + (TargetAngleX - AngleX) *0.35 
		local dist = TargetAngleY - AngleY 
		dist = math.abs(dist) > 180 and dist - (dist / math.abs(dist)) * 360 or dist 
		AngleY = (AngleY + dist *0.35) %360
		cam.CameraType = Enum.CameraType.Scriptable

		cam.CoordinateFrame = CFrame.new(head.Position.X, head.Position.Y + 0.1, head.Position.Z) 
			* CFrame.Angles(0,math.rad(AngleY),0) 
			* CFrame.Angles(math.rad(AngleX),0,0)
			* CFrame.new(0,0.5,0) -- offset

		humanoidpart.CFrame=CFrame.new(humanoidpart.Position)*CFrame.Angles(0,math.rad(AngleY),0)
	else game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
	end

	if (cam.Focus.p-cam.CoordinateFrame.p).magnitude < 1 then
		running = false
	else
		running = true
		--if freemouse == true then
		--	game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
		--else
		--	game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
		--end
	end
	--cam.CFrame = char.Head.CFrame

	--if not CanToggleMouse.allowed then
	--	freemouse = false
	--end

	--cam.FieldOfView = FieldOfView

end)

Camera Sway Script:

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera
local Turn = 0

local Lerp = function(a, b, t)
	return a + (b - a) * t
end;

RunService:BindToRenderStep("CameraSway", Enum.RenderPriority.Camera.Value + 1, function(deltaTime)
	local MouseDelta = UserInputService:GetMouseDelta()

	Turn = Lerp(Turn, math.clamp(MouseDelta.X, -3, 3), (3 * deltaTime))

	Camera.CFrame = Camera.CFrame * CFrame.Angles(0, 0, math.rad(Turn))
end)
2 Likes

Everything in the Camera Sway code seems right, except for the Lerp function imo.

I haven’t really worked with first person or trying to sway a camera, so I might not have a good solution.

Anyways, to the point, I think the reason why your Camera Sway code isn’t working is because of the Lerp code.

Maybe try changing it to this?:

function Lerp(a, b, t)
	return a + (b - a) * t
end
1 Like

It only doesnt work when Im running both scripts though.

2 Likes

SOLUTION:

I got it working. I had to change up the part of the code controlling the camera. The answer actually is pretty straight forward, kinda surprised it took me this long lol.

local MouseDelta = UIS:GetMouseDelta()
		local deltaTime = RS.Heartbeat:Wait()

		Turn = Lerp(Turn, math.clamp(MouseDelta.X, -3, 3), (3 * deltaTime))
		
		cam.CoordinateFrame = CFrame.new(head.Position.X, head.Position.Y + 0.1, head.Position.Z) 
			* CFrame.Angles(0,math.rad(AngleY),0) 
			* CFrame.Angles(math.rad(AngleX),0,0)
			* CFrame.new(0,0.5,0) -- offset
			* CFrame.Angles(0, 0, math.rad(Turn))

1 Like

Really nice looking camera, would it be ok if I used it for a game? I won’t if you don’t want people to copy it, but just wondering if I’m allowed.

Nah sorry man. Its for a personal project im working on.

That’s totally ok! :+1: I’ll probably experiment some Tool Box ones.

1 Like

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