Hello! Does someone know why is my camera not following the mouse? Something strange is happening; when I move the mouse, the camera doesn’t follow it, and I would like to fix that so that my first-person mode works properly. Can someone give me a hand, plz?
-- CameraSwitchModule
local CameraSwitchModule = {}
-- Constants
local FIRST_PERSON = Enum.CameraType.Scriptable
local THIRD_PERSON = Enum.CameraType.Custom
-- Variables
local player = game.Players.LocalPlayer
local currentCameraType = FIRST_PERSON
-- Functions
local function updateFirstPersonCamera()
-- Get the player's character and camera
local character = player.Character
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
-- Check if the character and camera exist
if character and camera then
-- Get the character's head
local head = character:FindFirstChild("Head")
-- Check if the head exists
if head then
-- Set the camera subject to the head
camera.CameraSubject = head
camera.CFrame = head.CFrame * CFrame.new(0, 0, -5) -- Adjust camera offset as needed
-- Change the camera type to Scriptable
camera.CameraType = Enum.CameraType.Scriptable
-- Make the head and its descendants transparent
local function makeTransparent(part)
if part:IsA("BasePart") then
part.LocalTransparencyModifier = 1
end
for _, child in pairs(part:GetChildren()) do
makeTransparent(child)
end
end
makeTransparent(head)
-- Make accessories transparent
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.LocalTransparencyModifier = 1
end
end
end
end
-- Variables for mouse movement
local sensitivity = 0.01
local acceleration = 10
local maxSpeed = 5
local speed = 0
local lastMousePosition = Vector2.new()
-- Before every frame is rendered, call this function
RunService.RenderStepped:Connect(function(deltaTime)
if currentCameraType == FIRST_PERSON then
-- Get the current mouse position
local currentMousePosition = UserInputService:GetMouseLocation()
-- Calculate the change in mouse position
local delta = currentMousePosition - lastMousePosition
-- Accelerate the camera rotation
speed = math.clamp(speed + acceleration * deltaTime, 0, maxSpeed)
-- Adjust the camera orientation based on mouse movement and speed
local rotationY = math.rad(delta.x * sensitivity * speed)
local newCFrame = CFrame.Angles(0, rotationY, 0)
camera.CFrame = head.CFrame * newCFrame
-- Update the last mouse position
lastMousePosition = currentMousePosition
end
end)
end
end
end
local function updateThirdPersonCamera()
-- Get the player's character and camera
local character = player.Character
local camera = workspace.CurrentCamera
-- Check if the character and camera exist
if character and camera then
-- Set the camera as a descendant of the character's humanoid root part
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
camera.CFrame = humanoidRootPart.CFrame * CFrame.new(0, 5, -10) -- Adjust camera offset as needed
end
end
end
function CameraSwitchModule.SetFirstPersonWithMouse()
-- Set the camera to first person
game.Workspace.CurrentCamera.CameraType = FIRST_PERSON
currentCameraType = FIRST_PERSON
-- Set the camera as a descendant of the head
updateFirstPersonCamera()
end
function CameraSwitchModule.SetThirdPerson()
-- Set the camera to third person
game.Workspace.CurrentCamera.CameraType = THIRD_PERSON
currentCameraType = THIRD_PERSON
-- Set the camera as a descendant of the humanoid root part
updateThirdPersonCamera()
end
function CameraSwitchModule.ToggleCamera()
-- Toggle between first and third person
if currentCameraType == FIRST_PERSON then
CameraSwitchModule.SetFirstPersonWithMouse()
else
CameraSwitchModule.SetThirdPerson()
end
end
return CameraSwitchModule
video: