How do i get the camera to follow the mouse

Ive been trying to make this script where the camera will follow the players mouse movement (EX: if the mouse moves up, so does the camera)

But no matter what i try it never works out. Heres my current script:

repeat task.wait(1) until game:IsLoaded() task.wait(1)

local rs = game:FindService("RunService") or game:GetService("RunService")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local camera = game.Workspace.CurrentCamera
local UIS = game:FindService("UserInputService") or game:GetService("UserInputService")
local bool = false

local char = plr.Character
local rootPart = char.HumanoidRootPart




UIS.InputBegan:Connect(function(input, chat)
	if chat then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton3 and bool == false then
		camera.CameraType = Enum.CameraType.Scriptable
		rs.Heartbeat:Connect(function(fps)
			local MPos = mouse.Hit.p
			camera.CFrame.Position.LookVector = MPos.LookVector

		end)
	end
end)

Ive looked all over devforum, and there are some helpful posts but everything ive tried hasnt worked. Help is appreciated, thank you for your time :grinning:

3 Likes

I believe the easiest way would be to make use of mouse.Hit and set the CFrame of the camera to CFrame.new(camera.Position, mouse.Hit)

To make it constantly follow, I would run it through a RenderStepped loop on the client

1 Like

Try this:

if not game:IsLoaded() then
	game.Loaded:Wait()
end

local rs = game:FindService("RunService") or game:GetService("RunService")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local camera = game.Workspace.CurrentCamera
local UIS = game:FindService("UserInputService") or game:GetService("UserInputService")
local bool = false

local char = plr.Character
local rootPart = char.HumanoidRootPart




UIS. InputChanged:Connect(function(input, chat)
	if chat then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton3 and bool == false then
		camera.CameraType = Enum.CameraType.Scriptable
		rs.RenderStepped:Connect(function(fps)
			local MPos = mouse.Hit.p
			camera.CFrame.Position.LookVector = MPos.LookVector

		end)
	end
end)

This worked! but its a little-

eye strain warning!!

1 Like

That gave me a good laugh, lol.

If you wanted to make it slower, I would probably Tween it to that position rather than set it immediately.

local TS = game:GetService("TweenService")
local camTurnInfo = TweenInfo.new(
    .5,
    Enum.EasingStyle.Quad,
    Enum.EasingDirection.Out,
    0,
    false,
    0
)

game:GetService("RunService").RenderStepped:Connect(function()
    local camera = workspace.CurrentCamera
    local moveCamera = TS:Create(
        camera,
        camTurnInfo,
        {CFrame = CFrame.new(camera.Position, mouse.Hit.p)
    )
end)

Let me know if this is any better. I haven’t tested it, but theoretically it should be a lot less… jittery

1 Like

WHY DIDNT I THINK OF THAAAAAT :sweat_smile:

Ill try it out!

2 Likes

It worked perfectly! thank you!! Id have to make the camera follow the player though

EDIT: Just had to change the camera type to do that

4 Likes

Was just about to type to change the camera type, but you figured it out! Love the Undertale screen btw

1 Like

Thank you!

one last question, how would I try and “lock” the mouse in its current position? I tried UIS.MouseBehaviour but that didnt work :sweat_smile:

So, for security reasons, you can’t lock a player’s actual mouse in whatever UDim2 position it was last at, though the need to do so isn’t too uncommon with certain battle and gameplay mechanics

My best advice if you want to achieve this is to make a custom mouse cursor with an ImageLabel, having it constantly set to the position of the player’s mouse, while using the MouseIconEnabled property of UserInputService to make the real mouse invisible. Then when you want to “lock” the mouse, make the ImageLabel stop moving

1 Like

hi! this worked!!

but im trying to make it togglable, and the camera wont revert to normal. I believe its because of RunService. Is there a way to make it toggleable?

so, an easy way to do this would be:

local toggle = false
RunService.RenderStepped:Connect(function()
    if toggle == false then
        -- do somethingg
    end
end)

When toggle is true, you can do whatever you need to do while the mouse is locked and then when you’re done, turn it back off

1 Like

where would i put toggle = true? it loops so it’ll constantly be switching from on to off

When your mouse is locked, you would set toggle to true. Then, when you’ve unlocked the mouse, you can set it back to false. RunService will still run, but it’ll only do stuff when toggle is set to false. This is the best way to have a toggling system with RenderStepped

1 Like