Y axis inaccuracy in my custom camera

i have a problem where when i try to move the camera while facing the front or the back of my character directly, the camera goes a bit diagonally for some reason

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

local BaseCamera = {}
BaseCamera.__index = BaseCamera

function BaseCamera.new()
	local self = setmetatable({}, BaseCamera)

	self.cameraType = "Scriptable"
	self.cameraSubject = nil
	self.cameraPos = nil
	self.cameraRot = nil

	self.offset = Vector3.new(0,0,8)
	self.targetAngle = Vector2.new(0,0)

	self.player = game.Players.LocalPlayer
	self.camera = workspace.CurrentCamera
	self.character = nil
	self.humanoid = nil
	self.rootPart = nil

	self.rightMouseDown = false
	self.mouseLock = false
	self.mouseDelta = nil
	self.sensitivity = 0.2
	self.zoomSpeed = 1
	self.minZoom = 4
	self.maxZoom = 16
	self.lastInputPos = nil

	self.rotationSmoothness = 10

	self.enabled = false
	self.connections = {}

	return self
end

function BaseCamera:Start()
	if self.enabled then return end
	self.enabled = true

	if not self.player.Character then
		self.player.CharacterAdded:Wait()
	end
	self:_setupCharacter(self.player.Character)

	self:_setupConnections()

	self.camera.CameraType = Enum.CameraType.Scriptable
	self.camera.CameraSubject = self.humanoid
end

function BaseCamera:Stop()
	if not self.enabled then return end
	self.enabled = false

	for _, connections in self.connections do
		connections:Disconnect()
	end
	self.connections = {}
end

function BaseCamera:_setupCharacter(character)
	self.character = character
	self.humanoid = character:WaitForChild("Humanoid")
	self.rootPart = character:WaitForChild("HumanoidRootPart")
	
	self.targetAngle = Vector2.new(0,0)
end

function BaseCamera:_setupConnections()
	table.insert(self.connections, self.player.CharacterAdded:Connect(function(character)
		self._setupCharacter(character)
	end))

	table.insert(self.connections, UIS.InputBegan:Connect(function(input, gpe)
		if gpe then return end

		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			self.rightMouseDown = true
		end
	end))

	table.insert(self.connections, UIS.InputEnded:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			self.rightMouseDown = false
		end
	end))

	table.insert(self.connections, UIS.InputChanged:Connect(function(input, gpe)
		if gpe then return end

		if input.UserInputType == Enum.UserInputType.MouseMovement then
			if self.rightMouseDown then
				UIS.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
				self.mouseLock = true

				self.mouseDelta = Vector2.new(input.Delta.X, input.Delta.Y)

				self.targetAngle = Vector2.new(self.targetAngle.X + (self.mouseDelta.X * self.sensitivity), math.clamp(self.targetAngle.Y + (self.mouseDelta.Y * self.sensitivity), -80, 69))
			else
				UIS.MouseBehavior = Enum.MouseBehavior.Default
				self.mouseLock = false
			end
		end
	end))

	table.insert(self.connections, RunService.RenderStepped:Connect(function(dt)
		local yaw = CFrame.Angles(0, math.rad(self.targetAngle.X), 0)
		local pitch = CFrame.Angles(math.rad(-self.targetAngle.Y), 0, 0)
		local cameraRot = yaw * pitch

		local cameraOffset = cameraRot * self.offset
		local cameraPos = self.rootPart.Position + Vector3.new(0, 3, 0) + cameraOffset
		local lookAtPos = self.rootPart.Position + Vector3.new(0, 1.5, 0)

		self.camera.CFrame = CFrame.lookAt(cameraPos, lookAtPos)
	end))
end

function BaseCamera:GetCameraInfo()
	return self
end

return BaseCamera

If its moving diagonally then its probably the Z orientation of the camera. In the regular roblox camera system the Z orientation is always 0. You could try first checking if the value of Z ever changes and to fix it either find where it sets the Z value or just set the Z to 0 manually whenever the CFrame is set.

1 Like

when i get the cameras rotation with ToEulerAnglesXYZ, Z is fluctuating around 0 and 3, did you mean that?

I redid the calculations and fixed it

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