Hello fellow developers!
I having a rather strange issue with the camera I’m making for my game - Drifter!
EDIT: Should probably include the game link, haha.
Here is a video showing the issue:
Take note how the car jitters when the car is moving at speed.
I don’t know what seems to be the problem. At first I thought it was a issue with performance but as you can see at the bottom right of the video - my CameraScript barely goes over 1% usage.
Here is the actual script:
(apologies for the poor-indentation - for some reason it won’t copy properly)
(UPDATED SCRIPT)
local cam = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local char = game.Workspace:WaitForChild(player.Name)
local hum = char:WaitForChild("Humanoid")
local pp = char.PrimaryPart or char:FindFirstChild("Head")
local rs = game:GetService("RunService")
repeat wait(.5) until hum.SeatPart and hum.SeatPart:IsA("VehicleSeat") and hum.SeatPart.Parent:FindFirstChild("Engine",true)
local seat = hum.SeatPart
local car = seat.Parent
local mp = car:FindFirstChild("Engine",true)
local carSize = car:GetExtentsSize().magnitude/10
local cOffset = CFrame.new(0,2,2)
local lastVel = Vector3.new()
local cf0,cf1
function Calc(dt)
local pos = mp.Position
local forward = mp.CFrame * CFrame.new(0,0,10).p
local backward = mp.CFrame * CFrame.new(0,0,-10).p
local vel = mp.Velocity
local pos2 = (pos+vel)
local speed = math.ceil((vel).magnitude * ((pos2-forward).magnitude > (pos2-backward).magnitude and 1 or -1))
local forwards = speed >= -5 and 1 or -1
local origin = mp.Position
local dir = (mp.CFrame.lookVector).unit*999 -- Car's facing direction
local vel = (mp.Velocity).unit*999 -- Car's velocity direction
--cf0 = CFrame.new(pos,pos + dir + vel / 100 * forwards) * cOffset
cf0 = mp.CFrame * CFrame.new(0,4 * carSize,8 * carSize * forwards) * CFrame.Angles(math.rad(-45 * carSize/5 * forwards),forwards < 0 and math.rad(180) or 0,0) * cOffset
cf1 = CFrame.new(pos,pos+(dir + vel / 100*forwards)) * CFrame.new(0,-5,-20 * forwards)
cf0 = cam.CFrame:Lerp(cf0:Lerp(cf1,0.1),0.1 + mp.Velocity.magnitude/1000)
--cf1 = cam.CFrame:Lerp(cf0:Lerp(cf1,0.6),0.2 + (mp.Velocity.magnitude/1000)) * CFrame.new(0,0,-10)
cam.CFrame = cf0--cam.CFrame:Lerp(cf0,0.8)
end
function Update(dt)
if hum.SeatPart and (hum.SeatPart:IsA("VehicleSeat")) then
if cam.CameraType ~= Enum.CameraType.Scriptable then
cam.CameraType = Enum.CameraType.Scriptable
end
Calc(dt)
elseif cam.CameraType == Enum.CameraType.Scriptable then
cam.CameraType = Enum.CameraType.Custom
end
end
rs:BindToRenderStep("VehicleCam",Enum.RenderPriority.Camera.Value-1,Update)
EDIT 2:
So after some experimenting I managed to improve the camera a little bit.
It doesn’t jitter as much, but I definitely don’t believe it has anything to do with the new Camera Scripts.
On my personal computer (that has decent specs) the jitter is negligiable. However when I switch vehicles to the Truck in my game, the jitter increases.
Even more so, when I use a laptop to run the same place - the jitter is exponentially worse.
I have a feeling its related to the physics usage in my game, but if that was the case, wouldn’t the default camera jitter all the same?
I’ve update the script with what I have at the moment.
Any insight would be greatly appreciated.