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

How do I fix my first person camera script?

I'm making a custom first person camera script. As apposed to using roblox's so I can add smooth head bobbing, crouching etc. However, I'm bad at 3-D math so I'm stuck on how to make the camera turn with the mouse. What happens is the camera starts twisting and going out of control.

while not game.Players.LocalPlayer.Character do wait() end

local cam = workspace.CurrentCamera
cam.CameraType = "Scriptable"

local plrChar = game.Players.LocalPlayer.Character

local plrHead = plrChar:waitForChild("Head")
plrHead.LocalTransparencyModifier = 1

local CAMERA_MOVE_FACTOR = 3

local xAngle = 0
local yAngle = 0


inputService.InputChanged:connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		if input.Delta.x < 0 then
			xAngle = xAngle - CAMERA_MOVE_FACTOR
		elseif input.Delta.x > 0 then		
			xAngle = xAngle + CAMERA_MOVE_FACTOR
		end
		
		if input.Delta.y < 0 then
			yAngle = yAngle - CAMERA_MOVE_FACTOR
		elseif input.Delta.y > 0 then		
			yAngle = yAngle + CAMERA_MOVE_FACTOR		
		end
	end
end)

game:GetService("RunService").RenderStepped:connect(function()
	print(CFrame.Angles(math.rad(-yAngle),math.rad(-xAngle),0))
	cam.CFrame = CFrame.new(plrHead.CFrame.p) * CFrame.Angles(math.rad(-yAngle),math.rad(-xAngle),0)
end)

2 comments

2

on 20, 22, 26 and 28, you might want to use multiply instead (eg xAngle * -CAMERA and xAngle *CAMERA)

0

Okay Ill try it!

1 answer

Line 8 is WaitForChild not waitForChild

2 comments

0

Uh... Thanks I guess, but “WaitForChild” isn’t really answering my question

0

Plus capitalization doesn’t really matter there

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