Hey, I’ve been trying to make a smooth camera that follows character on X axis relative to camera CFrame.
Example from Blood Engine game:
As you can see on the video, the camera position is slightly delayed from character on X axis, which I want to achieve.
However, I’ve faced the choppy camera shake when walking on X axis relative to camera:
Laggy camera:
Here is the code:
local Character = game.Players.LocalPlayer.Character
local Camera = workspace.CurrentCamera
-- Creating an anchored flying part
local tweenCamera = Instance.new("Part",module.character)
tweenCamera.Name = "TweenCamera"
tweenCamera.Anchored = true
tweenCamera.CanCollide = false
tweenCamera.CanTouch = false
tweenCamera.CanQuery = false
tweenCamera.Transparency = 0
tweenCamera.Size = Vector3.new(1,1,1)
-- Making the flying part CameraSubject
Camera.CameraSubject = tweenCamera
local HumanoidRootPart = Character.HumanoidRootPart
RunService.Stepped:Connect(function(_,deltaTime)
local startPos = tweenCamera.Position
local finishCF = HumanoidRootPart.CFrame * CFrame.new(2,0,0)
local finishPos = finishCF.Position
local magnitude = (startPos - finishPos).Magnitude
local distance = (2 + magnitude/10)
--[[ Calculating the distance to add for current frame, so it's faster when far,
smoother when close to character
]]
local deltaCF = Camera.CFrame:ToObjectSpace(HumanoidRootPart.CFrame)
tweenCamera.CFrame = Camera.CFrame * CFrame.new(tweenCamera.CFrame:ToObjectSpace(finishCF).X * distance,deltaCF.Y,deltaCF.Z)
--[[ Lerping the flying part X axis so it follows the character smoothly,
but keeping it's Y and Z axis same as Character's, so it follows
Character with delay only on X axis
]]
end)
I’ve tried everything. All RunService events, like Heartbeat, Stepped, RenderStepped, BindToRenderStepped with all priorities, PostSimulation etc.
Removing the deltaTime from math makes the shake more drastic.
The cause of the shake
This shake occurs due to the flying part positioning. And because the CameraSubject is the flying part, everything seems to shake, while only the flying part is actually shaking.
According to my experience, the flying part positioning is so “shaky” because the camera and character render separately every time. And if I ignore the camera CFrame in the math, everything will go smooth:
Smooth result, but it follows on each axis:
Here is the code:
RunService.Stepped:Connect(function(_,deltaTime)
local startPos = tweenCamera.Position
local finishPos = (HumanoidRootPart.CFrame * CFrame.new(2,0,0)).Position
local magnitude = (startPos - finishPos).Magnitude
local distance = (2 + magnitude/10)
tweenCamera.Position = tweenCamera.Position:Lerp(finishPos, math.min(deltaTime * distance,magnitude))
end)
As you can see, the shake occurs because of the desync between Character and Camera.
I need Camera to follow character smoothly only on X axis relative to it’s CFrame without the shake.
Any help is highly appreciated!