How can I get a top-down camera to move with the mouse?

I want my camera to be able to move along with the mouse as shown below.
2025-01-0111-16-13-ezgif.com-optimize

I’ve considered a few solutions such as lerping a camera part, but I’m not sure what would be the optimal solution.

1 Like

Is this what you’re looking for?

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

local distance = 50
local rangeMultiplier = 2

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local middleLocation: BasePart = workspace:WaitForChild("X") -- update this, you could have a platform for the mouse to run along as this.

camera.CameraSubject = middleLocation

RunService.RenderStepped:Connect(function()
	local mouse = player:GetMouse()
	local hitLocation = (mouse.Hit.Position * rangeMultiplier) - (middleLocation.Position)
	print(mouse.Hit.Position)
	camera.CFrame = CFrame.new(hitLocation + Vector3.yAxis * distance) * CFrame.Angles(math.rad(-90), 0, math.rad(-90))
	
	local p = Instance.new("Part")
	p.Size = Vector3.new(1,1,1)
	p.CFrame = camera.CFrame - (Vector3.yAxis * distance)
	p.CanCollide = false
	p.CanTouch = false
	p.CanQuery = false
	p.Transparency = 0.5
	p.Parent = workspace
end)

1 Like

That seems to be what I’m looking for, but I’m not too sure about the cloned parts flowing around as it moves or the boundary being set to just a part

Update: it is a bit jittery in certain spots when I set the “middleLocation” to HRP due to it now calculating the camera height from the HRP’s position. That would be my only complaint for that script, but thank you nonetheless. I will try to polish it before setting the solution.

1 Like

New issue I have encountered.
The further the player strays away from the “middleLocation”, the less centered they will get until eventually they are off the screen.

I apologize if I didn’t make this clear before.

1 Like

just set middlelocation to humanoidrootpart.position

1 Like

What is your “MiddleLocation” set to? I’m trying to recreate the issue with it being the HumanoidRootPart, but there seems to be no issue.

I changed local middleLocation: BasePart = workspace:WaitForChild("X") to local middleLocation: BasePart = LocalPlayer.Character.HumanoidRootPart.
I’m not sure what is going wrong here.

1 Like

Works fine for me. I don’t know what the issue could be.

1 Like

it appears that I can’t change the rangeMultiplier or else that issue occurs. It must be at 2.

2 Likes

Found the issue.
update this line:

local hitLocation = (mouse.Hit.Position * rangeMultiplier) - (middleLocation.Position)

to this:

local hitLocation = (mouse.Hit.Position * rangeMultiplier) - (middleLocation.Position * (rangeMultiplier - 1))

Hope this works.

3 Likes

There’s a new issue that I can’t seem to solve. The camera will instead base its distance from a wall instead of the HRP if the player is close enough to a wall, and the mouse is on the other side of the wall opposite the player.

Would it be possible to keep the distance between the player and the camera static?

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