Camera moves with your mouse in 2D view

Hello Developers!

I’ve been working on a game recently and it’s a sort of 2d game with a bit of a mix with 3d.
Anyways, what i want to tell you guys is that i’m stuck at a problem. Or atleast, i have no idea how i should make something what i want to achieve.

But what do i want to achieve?

You can see it by the title, i want it that the camera moves with your mouse.
But, i already have a script (Down here) located in StarterPlayerScripts that makes the camera view 2D and locked. (Check this video out to see what i mean). So in short; I want to add an extra feature to make the camera move a bit with your mouse

I hope someone could help me :smiley:

(Sorry if it’s very unclear, explaining isn’t really my best thing)

-- This is the 2D view script

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")

player.CharacterAdded:Wait()
player.Character:WaitForChild("HumanoidRootPart")
camera.CameraType = Enum.CameraType.Attach
camera.FieldOfView = 40
camera.DiagonalFieldOfView = 67
camera.MaxAxisFieldOfView = 72

RunService.Stepped:Connect(function()
	camera.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position) * CFrame.new(0, 5, 30)
end)

Did you want it that way?, place it in StarterCharacterScripts

local Players = game:GetService("Players").LocalPlayer
local char = script.Parent
local hrp = char:WaitForChild("HumanoidRootPart")
local Mouse = Players:GetMouse()
local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable

--X,Y max 
local MouseMove = true --> turn on/off move with mouse
local XCameraMAX= 10
local YCameraMAX = 8
local mouseX
local mouseY

--folder for camera
local PlayerCamera = Instance.new("Folder")
PlayerCamera.Parent = workspace
PlayerCamera.Name = "PlayerCamera"

--camera
local cameraPart = Instance.new("Part")
cameraPart.Transparency = 1
cameraPart.CanCollide = false
cameraPart.Parent = workspace.PlayerCamera
cameraPart.CFrame = CFrame.new(hrp.Position + Vector3.new(-30,10,0), hrp.Position) -->camera angle

--camera move
local bp = Instance.new("BodyPosition")
bp.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bp.P = 100000 --> camera speed
bp.Parent = cameraPart

--loop
game:GetService("RunService").RenderStepped:Connect(function()
	if MouseMove then
	    mouseX = XCameraMAX * (workspace.CurrentCamera.ViewportSize.X / 2 - Mouse.X) / workspace.CurrentCamera.ViewportSize.X / 2
		mouseY = YCameraMAX * (workspace.CurrentCamera.ViewportSize.Y / 2 - Mouse.Y) / workspace.CurrentCamera.ViewportSize.Y / 2
	else
		mouseX = 0
		mouseY = 0
	end
	
	bp.Position = hrp.Position + Vector3.new(-20, 6 + mouseY, 0 - mouseX) --> X = approach, Y = up down, Z = left right,
	cam.CFrame = cameraPart.CFrame
end)
1 Like

Yes but the controls are wrong, because when i press D, i walk forward. S i walk to the right. A i walk back (So to the camera) and with W i walk to the left, which is not good. But the script is good, except the movement is wrong.

Edit: Nevermind! I just needed to delete the old script!

Thank you for your help! You really helped me with this!

And how do you want to control it?

1 Like

It’s already fixed, I thank you very much that I’m putting your name on the Credits tab

Ok so good luck with your game.

Pls dont keep ingnoring my posts, help me on flying script