How would I make a camera follow the player's mouse? (Enter the Gungeon)

It looks like it happens because your cursor isn’t hitting anything. Since the mouse’s ray has a max 1000 stud? length, that’s why it happens. What if instead of using mousePos you used the mouse’s UnitRay property’s origin? It’s in world coordinates so that shouldn’t cause any issues, it might make the result a bit different than what you have now, though.

Why is noone using MS.X or MS.Y? I rewrote the script you gave to use the mouse 2D position instead of the mouse 3D position.

local RunService = game:GetService('RunService')

local Plr = game:GetService('Players').LocalPlayer
local MS = Plr:GetMouse()

local Camera = workspace.CurrentCamera

local CAMERA_DEPTH = 24
local HEIGHT_OFFSET = 1.5 -- 1.5 is the distance from the root part to the head

local function updateCamera()
	local Char = Plr.Character
	
	if Char then
		local rootPart:BasePart = Char:FindFirstChild("HumanoidRootPart")
		
		if rootPart then
			local Center = Camera.ViewportSize / 2
			local Offset = (Vector2.new(MS.X,MS.Y) - Center) / Camera.ViewportSize
			Offset *= 5 -- max 5 studs in all directions
			
			local Pos = (Vector3.new(0, HEIGHT_OFFSET, 0) + rootPart.Position) + Vector3.new(Offset.X,-Offset.Y,0)
			
			Camera.CFrame = CFrame.new(Pos.X, Pos.Y, rootPos.Z + CAMERA_DEPTH)
		end
	end
end

RunService:BindToRenderStep("SidescrollingCamera", Enum.RenderPriority.Camera.Value + 1, updateCamera)
1 Like

Sorry for the late reply, but…

This is amazing! Exactly what I was searching for.
I was unsure of how to use Mouse.X and Mouse.Y, since I believe they bring back an offset, similar to UIs. I’ll have to look more in depth into this script…
Either way, Thank you!

1 Like

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