Help with true first person camera

I am not very good with CFrame and Vector math and this requires a fair bit of it.

Very unintended results along the x axis:
https://gyazo.com/85cc0a66577af709dc49c66fab1a4ba6

Here is the code:

local FIRST_PERSON_OFFSET = Vector3.new(0, 0.85, -0.75)

local xAngle, yAngle = 0,0

Mouse.Moved:Connect(function(input)
	xAngle = xAngle - input.Delta.X
	yAngle = math.clamp(yAngle - input.Delta.Y, -80, 80)
end)

game:GetService("RunService").RenderStepped:Connect(function()
	local Character = Player.Character
	
	if (Character) then
		local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
			
		if (HumanoidRootPart) then
			local StartCFrame = (HumanoidRootPart.CFrame + HumanoidRootPart.CFrame:VectorToWorldSpace(FIRST_PERSON_OFFSET))
			local StartOrientation = StartCFrame*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)
			
			local CameraFocus = StartOrientation + StartOrientation:VectorToWorldSpace(Vector3.new(0, 0, -5))
				
			HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.Position, Vector3.new(CameraFocus.Position.X, HumanoidRootPart.Position.Y, CameraFocus.Position.Z))
				
			CurrentCamera.CFrame = CFrame.new(StartCFrame.Position, CameraFocus.Position)
		end
	end
end)
5 Likes

Hello,

Try this and paste it in local script in StarterGui :slight_smile:

local Player = game.Players.LocalPlayer
local character = (Player.Character or Player.CharacterAdded:wait())

game:GetService("RunService").RenderStepped:connect(function()
	if (character:FindFirstChild("Right Arm") and character:FindFirstChild("Left Arm")) then

		character:FindFirstChild("Right Arm").LocalTransparencyModifier = 0

		character:FindFirstChild("Left Arm").LocalTransparencyModifier = 0
		
		character:FindFirstChild("Right Leg").LocalTransparencyModifier = 0

		character:FindFirstChild("Left Leg").LocalTransparencyModifier = 0

		character:FindFirstChild("Torso").LocalTransparencyModifier = 0	
		
		character:FindFirstChild("Humanoid").CameraOffset = Vector3.new(0,0,-1)
		
		Player.CameraMode = 0
	    Player.CameraMode = 1
	end
end)

The StartCFrame should really just be a Vector3:

local StartPosition = HumanoidRootPart.Position + HumanoidRootPart.CFrame * FIRST_PERSON_OFFSET
local StartOrientation = CFrame.Angles(0, math.rad(xAngle), 0) * CFrame.Angles(math.rad(yAngle), 0, 0) + StartPosition

This should replace this line:

local StartCFrame = (HumanoidRootPart.CFrame + HumanoidRootPart.CFrame:VectorToWorldSpace(FIRST_PERSON_OFFSET))
local StartOrientation = StartCFrame*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)

This ain’t it chief https://gyazo.com/4120e503d08775d22b5e8170993fba69

1 Like

I want to use a custom camera setup instead of the humanoid’s. I also really don’t like that you have to manually set the transparency to 0 for every part every frame.

Oops, I think I got it:

local StartPosition = HumanoidRootPart.CFrame * FIRST_PERSON_OFFSET

Adding the HRP’s position to itself is never a good idea

This replaces this line, from the previous post:

local StartPosition = HumanoidRootPart.Position + HumanoidRootPart.CFrame * FIRST_PERSON_OFFSET

This works!

https://gyazo.com/c649877aad36bacec6766874eb346053?token=22c9ae7dc1eac420b23875921c8ddcce

Is there anyway I can make the HRP rotate as the camera rotates and not a frame behind?

Just make the HRP’s orientation the same as the xAngle, and add the HRP’s position to it.

HumanoidRootPart.CFrame = CFrame.Angles(0, math.rad(xAngle), 0) + HumanoidRootPart.Position

It’s a little stuttery now
https://gyazo.com/f931591d039e7a5669e834991ed179ed

What does your code look like so far?

Mouse.Moved:Connect(function(input)
	xAngle = xAngle - input.Delta.X
	yAngle = math.clamp(yAngle - input.Delta.Y, -80, 80)
end)

game:GetService("RunService").RenderStepped:Connect(function()
	local Character = Player.Character

	if (Character) then
		local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
		
		if (HumanoidRootPart) then
			local StartPosition = HumanoidRootPart.CFrame * FIRST_PERSON_OFFSET
			local StartOrientation = CFrame.Angles(0, math.rad(xAngle), 0) * CFrame.Angles(math.rad(yAngle), 0, 0) + StartPosition
			
			local CameraFocus = StartOrientation + StartOrientation:VectorToWorldSpace(Vector3.new(0, 0, -5))
			
			HumanoidRootPart.CFrame = CFrame.Angles(0, math.rad(xAngle), 0) + HumanoidRootPart.Position
			
			CurrentCamera.CFrame = CFrame.new(StartPosition, CameraFocus.Position)
		end
	end
end)

I think it may have to do with your CurrentCamera.CFrame definition.
Just use this and see if it makes it any less stuttery:

CurrentCamera.CFrame = StartOrientation

this eliminates your need for a CameraFocus variable, which was the whole reason why StartOrientation was created in the first place, so you may as well just substitute StartOrientation's value for the CurrentCamera:

CurrentCamera.CFrame = CFrame.Angles(0, math.rad(xAngle), 0) * CFrame.Angles(math.rad(yAngle), 0, 0) + StartPosition
2 Likes

https://gyazo.com/224371776cab5337fd76b4789082ffd3

HumanoidRootPart.CFrame = CFrame.Angles(0, math.rad(xAngle), 0) + HumanoidRootPart.Position
				
local StartPosition = HumanoidRootPart.CFrame * FIRST_PERSON_OFFSET
				
CurrentCamera.CFrame = CFrame.Angles(0, math.rad(xAngle), 0) * CFrame.Angles(math.rad(yAngle), 0, 0) + StartPosition

Also was getting another problem where I got the StartPosition before I set the HRP’s CFrame, but that seemed to fix it. Thanks!

3 Likes

If they go into water and start swimming it would be weird because the humanoidrootpart is facing where the camera is looking. So how do you fix that?