Whats required to acheive a smooth camera?

Well as the title suggests, I am trying to create a smooth camera system, kinda like the one in apeirophobia, where it just smoothly glides when you turn around. But I’m trying to achieve it in the most simplest way possible.

Lerping aka Linear Interpolation

yellow face man is right :+1:

Step-by-Step Guide

  1. Setup: Create a new LocalScript in StarterPlayerScripts to control the camera.
  2. TweenService: Use TweenService to smoothly transition the camera’s position and rotation.
  3. User Input: Capture user input to determine the desired camera movement.

Example Implementation

Step 1: Setting Up the LocalScript

Create a new LocalScript in StarterPlayerScripts and name it SmoothCamera.

Step 2: Script the Smooth Camera

Here’s a sample script that achieves smooth camera movement:

local players = game:GetService("Players")
local tweenService = game:GetService("TweenService")
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")

local player = players.LocalPlayer
local camera = game.Workspace.CurrentCamera

local targetCFrame = camera.CFrame
local tweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)

-- Function to smoothly update the camera position and rotation
local function updateCamera()
    local tween = tweenService:Create(camera, tweenInfo, {CFrame = targetCFrame})
    tween:Play()
end

-- Function to handle mouse movement
local function onMouseMove(input)
    if userInputService.MouseBehavior == Enum.MouseBehavior.LockCenter then
        local delta = input.Delta
        local sensitivity = 0.1 -- Adjust sensitivity as needed

        -- Calculate new camera rotation based on mouse movement
        local yaw = -delta.X * sensitivity
        local pitch = -delta.Y * sensitivity

        -- Apply rotation to the target CFrame
        targetCFrame = targetCFrame * CFrame.Angles(0, math.rad(yaw), 0)
        targetCFrame = CFrame.new(targetCFrame.Position) * CFrame.Angles(math.rad(pitch), 0, 0)

        updateCamera()
    end
end

-- Lock the mouse to the center of the screen for consistent camera movement
userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

-- Connect the mouse movement to the onMouseMove function
userInputService.InputChanged:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseMovement then
        onMouseMove(input)
    end
end)

-- Ensure the camera smoothly follows the character's head or any other part
runService.RenderStepped:Connect(function()
    local character = player.Character
    if character then
        local head = character:FindFirstChild("Head")
        if head then
            targetCFrame = CFrame.new(head.Position) * targetCFrame.Rotation
            updateCamera()
        end
    end
end)

bro stop giving people ai ahh responses

3 Likes

Please don’t just reply with AI generated responses. It is not helpful.

2 Likes

I tried it and it worked, thank you.

1 Like

this is a terrible script, it can be done with wayy less lines.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.