Is there any way to smoothen this camera?

I have a camera first-person system by various DevForum members helped me out with this, it works, but I ran into a small problem…

Expected Behaviour:
The camera should move more naturally…

Observed Behaviour:
The camera moves unnaturally, randomly up and down, which can make many people sick.

local UIS = game:GetService("UserInputService")
local Body = workspace["DAF XF"].Body
local Camera = workspace.Camera

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.One then
		Camera.CameraType = Enum.CameraType.Scriptable
		while Camera.CameraType == Enum.CameraType.Scriptable do
			task.wait()
			local pos = Vector3.new(Body.Parent.DriveSeat.Position.X, Body.Parent.DriveSeat.Position.Y + 3, Body.Parent.DriveSeat.Position.Z)
			local y = Body.Parent.DriveSeat.Orientation.Y - 0
			local x = Body.Parent.DriveSeat.Orientation.X - 0
			local z = Body.Parent.DriveSeat.Orientation.Z - 0
			Camera.CFrame = CFrame.new(pos) * CFrame.Angles(math.rad(x),math.rad(y),math.rad(z))
		end
	elseif input.KeyCode == Enum.KeyCode.Two then
		Camera.CameraType = Enum.CameraType.Custom
	end
end)
3 Likes

Blind guess …

local UIS = game:GetService("UserInputService")
local Body = workspace["DAF XF"].Body
local Camera = workspace.Camera

local targetPosition = Vector3.new()
local targetOrientation = CFrame.new()

local lerpSpeed = 0.1 -- Adjust this value to control the smoothness of movement

local function updateCamera()
    local currentCFrame = Camera.CFrame
    local newCFrame = CFrame.new(targetPosition) * targetOrientation
    Camera.CFrame = currentCFrame:Lerp(newCFrame, lerpSpeed)
end

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.One then
        Camera.CameraType = Enum.CameraType.Scriptable
        while Camera.CameraType == Enum.CameraType.Scriptable do
            task.wait()
            targetPosition = Vector3.new(Body.Parent.DriveSeat.Position.X, Body.Parent.DriveSeat.Position.Y + 3, Body.Parent.DriveSeat.Position.Z)
            local y = Body.Parent.DriveSeat.Orientation.Y - 0
            local x = Body.Parent.DriveSeat.Orientation.X - 0
            local z = Body.Parent.DriveSeat.Orientation.Z - 0
            targetOrientation = CFrame.Angles(math.rad(x), math.rad(y), math.rad(z))
            updateCamera()
        end
    elseif input.KeyCode == Enum.KeyCode.Two then
        Camera.CameraType = Enum.CameraType.Custom
    end
end)
4 Likes

The script is nice, but…
Sorry for being less specific.
The lerp slows the camera, but it is kinda still the same result.
I want the camera to be a little bit tilted down, and when I tilt the camera down, it just goes up and down randomly…
Sorry for the inconvenience…

2 Likes

You can introduce some controlled randomness to simulate a more dynamic and natural camera movement. Here’s an example of how you might achieve this effect:

local UIS = game:GetService("UserInputService")
local Body = workspace["DAF XF"].Body
local Camera = workspace.Camera

local targetPosition = Vector3.new()
local targetOrientation = CFrame.new()

local lerpSpeed = 0.1 -- Adjust this value to control the smoothness of movement
local maxRandomOffset = 0.5 -- Adjust this value for the maximum random offset

local function updateCamera()
    local currentCFrame = Camera.CFrame
    local newCFrame = CFrame.new(targetPosition) * targetOrientation
    Camera.CFrame = currentCFrame:Lerp(newCFrame, lerpSpeed)
end

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.One then
        Camera.CameraType = Enum.CameraType.Scriptable
        local lastTick = tick()
        while Camera.CameraType == Enum.CameraType.Scriptable do
            task.wait()
            local deltaTime = tick() - lastTick
            lastTick = tick()

            local driverSeat = Body.Parent.DriveSeat
            if driverSeat then
                local driverPos = driverSeat.Position
                targetPosition = driverPos + Vector3.new(0, 3, 0) -- Slightly above the driver seat
                local y = driverSeat.Orientation.Y - 45 -- Adjust this angle
                local x = driverSeat.Orientation.X - 15 -- Adjust this angle
                local z = driverSeat.Orientation.Z - 0

                -- Introduce randomness to the targetOrientation
                local randomOffset = CFrame.Angles(
                    math.rad(math.random(-maxRandomOffset, maxRandomOffset)),
                    math.rad(math.random(-maxRandomOffset, maxRandomOffset)),
                    math.rad(math.random(-maxRandomOffset, maxRandomOffset))
                )

                targetOrientation = CFrame.Angles(math.rad(x), math.rad(y), math.rad(z)) * randomOffset
                updateCamera()
            end
        end
    elseif input.KeyCode == Enum.KeyCode.Two then
        local currentCFrame = Camera.CFrame
        local newCFrame = CFrame.new(targetPosition) * targetOrientation
        Camera.CameraType = Enum.CameraType.Custom
        Camera.CFrame = currentCFrame:Lerp(newCFrame, lerpSpeed)
    end
end)
2 Likes

Nope. This is the result:

1 Like

That looks epic … and a lot more real. I really hate guessing, there should be enough there to mess with and figure this out … You can adjust the effects.

2 Likes

Well yeah, but when you are driving your car, you arent rotating your head like that…

EDIT: Im looking more for the Euro Truck Simulator 2 effect…

1 Like