Camera following mouse breaks everything!

hi everyone, so basically, i am making a main menu for my game. I have a camera part that sets up a nice shot for my game that is displayed as the background for my main menu.

however, I also want a feature of the main menu to be that the camera follows the mouse subtly, as it adds a nice touch. if you guys know what I’m talking about, skip this paragraph. If you don’t know what I’m talking about, it is when the camera stays in the same position it is set to, but when the player moves their mouse around, the camera follows with it, but it doesn’t leave the fixed position. hopefully you understood lol

when I try to implement this feature, the camera background just breaks completely. It shows me a black screen and when I traced the coordinates to it, it was -989.503, 105.265, -297.632, instead of the designated camPart part.

no errors in output.

if anyone knows why this is happening, I would really appreciate it! :smiley:

--HANDLE CAMERA
repeat
	camera.CameraType = Enum.CameraType.Scriptable
until camera.CameraType == Enum.CameraType.Scriptable

camera.CameraSubject = part
camera.CFrame = part.CFrame

local mouse = plr:GetMouse()
local scale = 200

function update()
	if plr.PlayerGui:WaitForChild("Menu (wip)").Enabled == false then
		return
	else
		--get the center of the screeen
		local center = Vector2.new(camera.ViewportSize.X/2, camera.ViewportSize.Y/2)
		--get the movement in studs (the mouse properties are in pixels, so you want to divide it by your scale to not move the camera really really far)
		local MoveVector = Vector3.new((mouse.X-center.X)/scale, (mouse.Y-center.Y)/scale, 0)
		--CFrame * CFrame makes it work the same regardless of rotation, where addition just makes it work for one direction
		camera.CFrame = camera.CFrame * CFrame.new(camera.CFrame.Position + MoveVector)
	end
end

game:GetService("RunService").RenderStepped:Connect(update) --fire function
1 Like

camera.CFrame = camera.CFrame * CFrame.new(camera.CFrame.Position + MoveVector) use CFrame.Angles instead to rotate it (if so then remember to use math.rad()) or set lookvector (camera.CFrame = CFrame.new(camera.CFrame.Position, [[where to look at (can be found by mouse.Hit, etc)]]))

theres a potential issue in your code which is that you dont reset the cam pos every frame so it may just keep rotating as past rotations arent negated and it builds up

1 Like

Thank you, i fixed it using CFrame.Angles like you suggested! :smiley:

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