How can I make my camera follow the mouse in first person?

Hello! How can I make my camera follow the mouse in first person? Similar to the camera movements in Roblox war games, I would like the camera follow the mouse movements (looking in the direction the mouse moves). How can I achieve this?

Module script

-- FirstPersonModule

local Module = {}

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

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local head = nil
local isInFirstPerson = true
local renderSteppedConnection

-- Función para actualizar la posición de la cámara
local function updateCamera()
	if head then
		-- Obtener la posición de la cabeza del jugador
		local headPosition = head.Position

		-- Ajustar la posición de la cámara a la posición de la cabeza
		camera.CFrame = CFrame.new(headPosition)
	end
end

-- Función para hacer invisibles los accesorios del jugador
function Module.makeAccessoriesInvisible()
	if player.Character then
		local character = player.Character

		for _, accessory in pairs(character:GetChildren()) do
			if accessory:IsA("Accessory") then
				for _, part in pairs(accessory:GetDescendants()) do
					if part:IsA("BasePart") then
						part.Transparency = isInFirstPerson and 1 or 0
					end
				end
			end
		end
	end
end

-- Función para activar el modo primera persona
function Module.setupFirstPersonMode()
	if not isInFirstPerson then
		isInFirstPerson = true
		Module.makeAccessoriesInvisible()
		renderSteppedConnection = RunService.RenderStepped:Connect(updateCamera)
	end
end

-- Función para activar el modo tercera persona
function Module.setupThirdPersonMode()
	if isInFirstPerson then
		isInFirstPerson = false
		Module.makeAccessoriesInvisible()
		if renderSteppedConnection then
			renderSteppedConnection:Disconnect()
			renderSteppedConnection = nil
		end
	end
end

-- Función para verificar si está en primera persona
function Module.isInFirstPerson()
	return isInFirstPerson
end

-- Activar primera persona por defecto
Module.setupFirstPersonMode()

-- Esperar a que el personaje del jugador esté disponible
player.CharacterAdded:Connect(function(character)
	-- Esperar a que la cabeza del jugador esté disponible
	character:WaitForChild("Head")

	-- Obtener la cabeza del jugador
	head = character.Head
end)

-- Manejar el caso en que el personaje ya exista cuando el jugador se une al juego
if player.Character then
	-- Esperar a que la cabeza del jugador esté disponible
	player.Character:WaitForChild("Head")

	-- Obtener la cabeza del jugador
	head = player.Character.Head
end

return Module

localscript

-- LocalScript

-- Obtener servicios necesarios
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

-- Obtener el módulo
local FirstPersonModule = require(game.ReplicatedStorage:WaitForChild("FirstPersonModule"))

-- Configurar variables
local player = Players.LocalPlayer

-- Forzar el inicio en primera persona
FirstPersonModule.setupFirstPersonMode()

-- Cambiar entre primera y tercera persona al presionar la tecla "E"
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.E then
		if FirstPersonModule.isInFirstPerson() then
			FirstPersonModule.setupThirdPersonMode()
		else
			FirstPersonModule.setupFirstPersonMode()
		end
	end
end)
2 Likes

Doesn’t it do that already? Like you look where your mouse is

No, I have not been able to do it… currently the camera is only fixed on the player’s head

1 Like

No, I have not been able to do it… currently the camera is only fixed on the player’s head

1 Like

Have you changed the camera movement in any other way then?

1 Like

The camera movement is only adjusted to follow the player’s head position in the first-person perspective…

1 Like

I tried it but player rotate on his own

-- Function to update the position and orientation of the camera following the mouse
local function updateCamera()
    if isInFirstPerson and head then
        -- Get the mouse position on the screen
        local mousePosition = UserInputService:GetMouseLocation()

        -- Convert the mouse position to a ray in the world
        local ray = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y, 1)

        -- Calculate the player's head position
        local headPosition = ray.Origin + ray.Direction * (head.Position - ray.Origin).Magnitude

        -- Calculate the direction from the head to the mouse position
        local lookVector = (headPosition - head.Position).unit

        -- Calculate the rotation needed to look towards the mouse position
        local newRotation = CFrame.new(Vector3.new(), lookVector)

        -- Adjust the camera orientation
        camera.CFrame = CFrame.new(head.Position) * newRotation

        -- Lock the player's rotation
        player.Character:FindFirstChildOfClass("Humanoid").AutoRotate = false
    end
end
1 Like

I think what you wanna achieve is locking the mouse in the center so you would have to do this

UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter