Hello! I’m currently working on a snake movement system and came upon a weird problem with DeltaTime / FPS. If I don’t use DT (DeltaTime) the lower my FPS the slowest I move, but if I use DT the lower my FPS the fastest I move.
Weird right? All I want is to be able to move within the same speed no matter how high or how low my FPS is. Take a look at the following examples:
[Code] Before DeltaTime:
local moveSpeed = 24
local rotationDelay = 0.1
local EE_linear = Enum.EasingStyle.Linear
local timer = 0
local interval = 1/60
--[[CODE]]--
--[[MOVEMENT SYSTEM]]--
local function UpdateMovement(dt)
-- Target pos and then tween to it
local targetPosition = Vector3.new(mouse.Hit.X,char.PrimaryPart.Position.Y,mouse.Hit.Z)
local rotateTween = tweenService:Create(char.PrimaryPart, TweenInfo.new(rotationDelay, EE_linear),
{CFrame = CFrame.lookAt(char.PrimaryPart.Position, targetPosition)}):Play()
-- PROBLEM IS PROB HERE, IT CONSTANTLY MOVES THE PLAYER TO LookVector
--(The front path the char is facing) times the moveSpeed
linear.VectorVelocity = char.PrimaryPart.CFrame.LookVector * moveSpeed
end
[Video] Before DeltaTime:
robloxapp-20231211-1931394.wmv (3.4 MB)
"Btw roblox doesn’t record the FPS Statistics but It always gets to 30 less FPS when I turn max settings on "
You can notice there’s slight diference in the speed whenever my FPS goes low (U can’t notice by the video but its twice slower). Take a look at it after I multiply by DeltaTime.
[Code] After DeltaTime:
local moveSpeed = 1008 -- (48) * 24 So it's the same speed as before
local rotationDelay = 0.1
local EE_linear = Enum.EasingStyle.Linear
local timer = 0
local interval = 1/60
--[[CODE]]--
--[[MOVEMENT SYSTEM]]--
local function UpdateMovement(dt)
-- Target pos and then tween to it
local targetPosition = Vector3.new(mouse.Hit.X,char.PrimaryPart.Position.Y,mouse.Hit.Z)
local rotateTween = tweenService:Create(char.PrimaryPart, TweenInfo.new(rotationDelay, EE_linear),
{CFrame = CFrame.lookAt(char.PrimaryPart.Position, targetPosition)}):Play()
-- PROBLEM IS PROB HERE, IT CONSTANTLY MOVES THE PLAYER TO LookVector
--(The front path the char is facing) times the moveSpeed
linear.VectorVelocity = char.PrimaryPart.CFrame.LookVector * moveSpeed * dt -- ADDED DeltaTime
end
[Video] After DeltaTime:
“Again, It doesn’t show the FPS but the speed difference is extremely noticeable. Btw sorry about the video quality”
robloxapp-20231211-1851539.wmv (3.4 MB)
“If it is of any help here’s the script that moves the body and how the char is made”
Char:
Body Code:
local Heartbeat = function(dt)
timer += dt
if timer >= interval then
for character, data in pairs(charData) do
if (character.PrimaryPart.Position - data.position).Magnitude < .5 then return end
local part = table.remove(data.parts, 1)
if part ~= nil then
table.insert(data.parts, part)
part.Position = data.position
data.position = char.PrimaryPart.Position
end
end
timer = 0
end
end
What I tried to do that didn’t work:
I tried making my own (dt) by using tick():
local lastUpdateTime = tick()
runService.RenderStepped:Connect(function()
local currentTime = tick()
local dt = currentTime - lastUpdateTime
lastUpdateTime = currentTime
UpdateMovement(dt)
end)
I tried to divide dt by magic numbers (LOL I had to try):
linear.VectorVelocity = char.PrimaryPart.CFrame.LookVector * moveSpeed * (dt / 2) -- or * / 3,4,5
I searched throught the forum but coudn’t find anything directly related to this in specific. Do you guys have any idea on why this is happening and how I could fix it?