How would I make the camera follow the mouse

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make the player’s camera follow where he is pointing his mouse
  2. What is the issue? Include screenshots / videos if possible!
    I can’t seem to make the camera follow the mouse’s position.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried looking at the dev forums and topics similar to it and even YouTube videos.
local userinput = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local function KeyDown()
	return userinput:IsKeyDown(Enum.KeyCode.J)	
end
local debounce = false
local canTouch = false
local Mouse = Player:GetMouse()
local character = Player.Character or Player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local RootPart = character:WaitForChild("HumanoidRootPart")
local RS  = game:GetService("RunService")
--local Player = game:GetService("Players").LocalPlayer
local controls = require(Player.PlayerScripts.PlayerModule):GetControls()

function beginLooking(input, gameproccessed)
	if gameproccessed then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.J and debounce == false then
			print("hi")
			local MoveMouseFunction
			MoveMouseFunction = Mouse.Move:Connect(function()
				local direction = (Mouse.Hit.p - character.HumanoidRootPart.Position) * Vector3.new(1,1,1)
				character.HumanoidRootPart.CFrame = CFrame.new(character.HumanoidRootPart.Position, character.HumanoidRootPart.Position + direction)
				if not KeyDown() then
					MoveMouseFunction:Disconnect()
				end
			end)
		end
	end
end

			
function beginAttack(input, gameproccessed)
	if gameproccessed then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.J and canTouch == false then
			canTouch = true
			print("Iknow")
			game.ReplicatedStorage.FireVortex:FireServer()

			local BP = Instance.new("BodyPosition")
			BP.D = 250 
			BP.MaxForce = Vector3.new(1e5,0,1e5) 
			BP.P = 1e4 
			BP.Position = RootPart.Position 
			local BG = Instance.new("BodyGyro")
			BG.CFrame = RootPart.CFrame 
			BG.D = 250 
			BG.MaxTorque = Vector3.new(1e5,0,1e5) 
			BG.P = 1e4
			BG.Parent = RootPart

			local RSconn
			RSconn = RS.Heartbeat:Connect(function()
				BG.CFrame = CFrame.new(RootPart.Position,Mouse.Hit.p)
			end)
			--controls:Disable()
			--[[userinput.InputEnded:connect(function(input)
				if input.KeyCode == Enum.KeyCode.J then 
					RSconn:Disconnect()
					BG:Destroy()
					return
				end
			end)--]]
			wait(7)
			--controls:Enable()
			canTouch = false
			BP:Destroy()
			BG:Destroy()
			RSconn:Disconnect()
		end
	end
end

userinput.InputBegan:Connect(beginLooking)
userinput.InputEnded:Connect(beginAttack)

I didn’t look much through your code block, but here is the general idea:

  • Make a CFrame position for the camera with the “at” position and the “lookAt” position (aka position to look towards)
  • Get the lookAt position with Mouse.Hit
  • IDK where you want your at position to be, below I have it at the camera’s position

Example:

local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")

local camera = Workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable

RunService.RenderStepped:Connect(function()
	local hit = Mouse.Hit
	if not hit then
		return
	end
	local lookAt = hit.Position
	local at = camera.CFrame.Position
	camera.CFrame = CFrame.new(at, lookAt)
end)

So for your last bullet. I just want the position to be at the mouse’s position. How would I do that?

Let me rephrase that, you have two positions:

  • One where the camera is at, kind of like where the eyes are
  • And another where the camera is looking at, kind of like what the eyes are focusing on

To get the position where the mouse is, use Mouse.Hit.Position. Remember to check if the Hit CFrame exists first though :+1:

To make a cframe from where the camera is at and what it should be looking at, use: CFrame.new(at, lookingAt)

This is what happens when I make at the root part
https://gyazo.com/22def31398eefe04cdab64a26fb397e7
I’m trying to make it so that it is maybe a bit behind this
https://gyazo.com/bbb381c70916d706e77662b27d7873f3
This is a vortex that I bring from replicated storage by pressing “J” how do I make it so that my camera follows where I’m going to. I can’t make the “at” the vortex because this is in the local script so there is no way for me to call on it