How do I make the camera face the direction of the mouse

Hello, I’m trying to make it so when the player moves the mouse to a certain direction of the screen the camera moves a tiny bit to the direction of the mouse. This the layout I’m trying to use the code on:

3 Likes

First you need to get the mouse instance which can be done as such:

local Mouse = Player:GetMouse()

Now that you have the mouse you are now able to get 2 very important position variables which will allow you to manipulate the camera:

local X = Mouse.X --Mouse's X position in a integer form
local Y = Mouse.Z

Now that you have a way of detecting the mouse’s location you can now manipulate the camera

First ensure that your CameraType is set to Enum.CameraType.Scriptable

function OnMove()
	local OriginPoint = Vector3.new(0, 0, 0) --Where your camera will sit
	local Focus = Vector3.new(5, 0, 0) --The position your camera will look at
	
	local X = Mouse.X / 1000 --Make the numbers smaller so they can be applicable
	local Y = Mouse.Y / 1000

	local GoalPoint = Vector3.new(OriginPoint.X + X, OriginPoint.Y + Y, OriginPoint.Z) --Calculate the position
	local GoalCFrame = CFrame.new(GoalPoint, Focus.Position) --Point the position at the focus point
	Camera.CFrame = GoalCFrame --Set the camera's CFrame
end

Mouse.Move:Connect(OnMove) --Will fire the OnMove function when the mouse moves
6 Likes

Thank you, Your script had a few bugs and I also tweaked it a bit for my preference. For anyone wanting to use the script that doesn’t give you an error here it is.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local CAMERA1 = game.Workspace:WaitForChild("Menu [CAMERAS]").CAMERA1

local X = Mouse.X --Mouse's X position in a integer form
local Y = Mouse.Y

function OnMove()
	local OriginPoint = Vector3.new(-21.5, 5.5, -0) --Where your camera will sit
	local Focus = Vector3.new(5, 0, 0) --The position your camera will look at

	local X = Mouse.X / 750 --Make the numbers smaller so they can be applicable
	local Y = Mouse.Y / 750

	local GoalPoint = Vector3.new(OriginPoint.X + Y, OriginPoint.Y + X, OriginPoint.Z) --Calculate the position
	local GoalCFrame = CFrame.new(GoalPoint, Focus) --Point the position at the focus point
	CAMERA1.CFrame = GoalCFrame --Set the camera's CFrame
end

Mouse.Move:Connect(OnMove) --Will fire the OnMove function when the mouse moves
5 Likes