Help making Camera follow mouse

Hello again amazing developers!
I’ m making a fan game and I want it to be as similiar as possible, one of the things inside the game is the camera following mouse and I was tryng to replicate that but then i realized that I had no idea how to make it and every solution or tutorial i tried ended up not being what i wanted or just broken.

This is a video of how i want it to look like:

If any of you blessed souls is able to help please do! Thank you.

6 Likes

You can try locking the mouse in the center of the screen and setting the mouse icon so it’s invisible using UserInputService. I guess something like this.

-- in a LocalScript or a Script in Client RunContext,
local UserInputService = game:GetService("UserInputService")

local isMouseLocked = false

local function updateMouseLock()
	UserInputService.MouseBehavior = if isMouseLocked
		then Enum.MouseBehavior.LockCenter
		else Enum.MouseBehavior.Default

	UserInputService.MouseIconEnabled = not isMouseLocked
end

local function setMouseLock(isLocked)
	isMouseLocked = isLocked
	updateMouseLock()
end

UserInputService:GetPropertyChangedSignal("MouseBehavior"):Connect(updateMouseLock)
UserInputService:GetPropertyChangedSignal("MouseIconEnabled"):Connect(updateMouseLock)

-- in a ModuleScript, 
-- return setMouseLock

The function setMouseLock should let you lock and unlock the mouse from center freely, where locking the mouse should make the camera follow its movements.

Edit: I just tested this in Studio, the above script should work.

1 Like

Unfortunately Roblox does not have a built in camera mode for achieving something like this automatically. To get the result you’re looking for, there would be multiple steps.

1. Lock the mouse (as mentioned in the previous post)

2. Change the CameraMode to Scriptable

3. Link a function to RunService that adjusts the players camera to a focused part and move the position of the camera according to player mouse movement

To save you the time, I can give you this: Camera.lua (7.7 KB) which I made a while back. It uses a lot of basic cframe manipulating equations to put the camera where it needs to go. Just require it and call Camera:SetMode(‘Orient’) and it should give you an orienting camera around the player.

1 Like

Many similar posts exists.

I know and i checked a lot of them but most didn’ t work and the rest wasn’ t what i was looking for… I wouldn’ t have made this post if i didn’ t check thousands of posts regarding the same issue :frowning:

Unfortunately this doesn’ t work and gives me an error i won’ try to fix it as I’ m very skittish about the idea of using someone elses module blindly without even understand it. Thanks for the help anyways tho!

No idea how i would use this and definetly not what I’ m looking for :slightly_frowning_face:
Thank you anyways though!

Would you mind sending a screenshot of the error? I can fix it real quick so that anyone seeing this post in the future can use it.

PrimaryPart error use WaitForChild and variables with checks to fix that easily

Once you call setMouseLock(true), my script makes the camera move whenever the mouse moves. That looks like what you’re trying to show in your video from the OP.

If the interface is confusing to you, the only function you would need to call is setMouseLock with a boolean argument.

  • setMouseLock(true) makes the camera’s orientation change by moving the mouse, kind of like how the mouse behaves when you zoom into first person.

  • setMouseLock(false) returns to default controls, where only holding RMB lets you orient the camera.

Here’s a simplified script that locks the mouse by default, which is equivalent to calling setMouseLock(true).

-- in a LocalScript,
local UserInputService = game:GetService("UserInputService")

local function lockMouse()
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	UserInputService.MouseIconEnabled = false
end

UserInputService:GetPropertyChangedSignal("MouseBehavior"):Connect(lockMouse)
UserInputService:GetPropertyChangedSignal("MouseIconEnabled"):Connect(lockMouse)

lockMouse()
1 Like

I did some looking around I think I found your fix in the documentation:

This is a local script no other camera edits are needed.

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

local camera = workspace.CurrentCamera
local cameraOffset = Vector3.new(2, 2, 8)
local player = Players.LocalPlayer

player.CharacterAdded:Connect(function(character)

	local humanoid = character:WaitForChild("Humanoid")
	local rootPart = character:WaitForChild("HumanoidRootPart")
	humanoid.AutoRotate = false

	local cameraAngleX = 0
	local cameraAngleY = 0

	local function playerInput(actionName, inputState, inputObject)
		-- Calculate camera/player rotation on input change
		if inputState == Enum.UserInputState.Change then
			cameraAngleX = cameraAngleX - inputObject.Delta.X
			-- Reduce vertical mouse/touch sensitivity and clamp vertical axis
			cameraAngleY = math.clamp(cameraAngleY-inputObject.Delta.Y*0.4, -75, 75)
			-- Rotate root part CFrame by X delta
			rootPart.CFrame = rootPart.CFrame * CFrame.Angles(0, math.rad(-inputObject.Delta.X), 0)
		end
	end
	ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)

	RunService.RenderStepped:Connect(function()
		if camera.CameraType ~= Enum.CameraType.Scriptable then
			camera.CameraType = Enum.CameraType.Scriptable
		end
		local startCFrame = CFrame.new((rootPart.CFrame.Position)) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
		local cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, cameraOffset.Z))
		local cameraFocus = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, -10000))
		camera.CFrame = CFrame.new(cameraCFrame.Position, cameraFocus.Position)
	end)
end)

local function focusControl(actionName, inputState, inputObject)
	-- Lock and hide mouse icon on input began
	if inputState == Enum.UserInputState.Begin then
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		UserInputService.MouseIconEnabled = false
		ContextActionService:UnbindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)
	end
end
ContextActionService:BindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)
3 Likes

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