Help with Camera positioning at Head

I’ve written this code up so the camera offset changes based on the location of where the eyes using RenderStepped. It works well enough, except for the fact that when you turn the character around, the camera offset doesn’t change quick enough. I’ve tried a few things like BindToRenderStepped, but it doesn’t seem to work.

Video of issue: https://youtu.be/X8o5biVZEqQ

Code (Held inside of a module script):

local CurrentCamera = workspace.CurrentCamera

local LocalPlayer = game:GetService("Players").LocalPlayer

return function()
	
	local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

	local Humanoid = Character:WaitForChild("Humanoid")

	local RootPart = Character:WaitForChild("HumanoidRootPart")

	local Head = Character:WaitForChild("Head")
	
	local Eyes = Character:WaitForChild("Eyes")

	if Humanoid.Health > 0 then

		if CurrentCamera.CameraType ~= "Custom" then CurrentCamera.CameraType = "Custom" end

		if Eyes and RootPart then

			Humanoid.CameraOffset = (RootPart.CFrame + Vector3.new(0, 1.2, 0)):PointToObjectSpace(Eyes.Position)

		end

	end	

	if Humanoid:GetState() == Enum.HumanoidStateType.Physics or Humanoid.Health <= 0 then

		if CurrentCamera.CameraType ~= "Scriptable" then CurrentCamera.CameraType = "Scriptable" end

		if Head then CurrentCamera.CFrame = Head.CFrame end

	end
	
end

Help would be appreciated, thanks.

1 Like

Migration Note
This event has been superseded by PreRender which should be used for new work.

I think the issue lies here

Tried both prerender and presimulation and it doesn’t fix issue. Still obvious when you flail your mouse around

The issue seems to be the indexing of the eyes.Position as parts only update on post simulation while the camera is on renderstep hence causing the delay.

Solution, IDK personally, however what you are doing seems to be similar to Realism and this was how they seemed to do it instead of indexing the position value they worked with the rotation value to get that offset.

function FirstPerson.GetSubjectPosition(self: IBaseCamera)
	if FirstPerson.IsInFirstPerson() then
		local camera = workspace.CurrentCamera
		local subject = camera.CameraSubject

		if subject and subject:IsA("Humanoid") and subject.Health > 0 then
			local character = subject.Parent
			local head = character and character:FindFirstChild("Head")

			if head and head:IsA("BasePart") then
				local cf = head.CFrame
				local offset = cf * CFrame.new(0, head.Size.Y / 3, 0)

				return offset.Position, cf.LookVector
			end
		end
	end

	local getBase: any = assert(self.GetBaseSubjectPosition)
	return getBase(self)
end

Using Realism, it doesn’t completely stay with the character similar to my script, however it seems like the camera is using a seperate axis (if that makes sense) when rotating, so the body can slowly rotate to where the camera is facing. This is not really what I want to achieve. I want it to be like the default roblox first person camera, where your body is always facing where the camera is

The issue I eventually found was actually with CameraOffset itself, which is laggy even with no extra calculations. The only way I fixed this was disabling auto rotate and creating a new auto rotate script for the player. This worked and also gave me the freedom to Lerp it, making it smooth similar to Realism

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