As the title implies I have having problems with my client-sided physics. It is entirely UI physics and my issue is coming primarily from the velocity of the frame.
Here is a gist of the code
local vel 0
local dir = -1
local accel = 1.4 -- Constant. Is
local lastdirchange = tick() -- Is used to determine the last direction change.
local pbar = ui:WaitForChild("PlayerBar") -- Simply just the UI frame that the player controls.
local connect
connect = game:GetService("RunService").RenderStepped:Connect(function(delta)
vel = math.clamp(vel + (dir*accel)*(tick()-lastdirchange), -16, 16)
pbar.Position = UDim2.new(math.clamp(pbar.Position.X.Scale+(vel*0.001), 0+pbar.Size.X.Scale/2, 1-pbar.Size.X.Scale/2), 0, 0.5, 0)
end)
-- The rest of this long code is relatively unrelated as the velocity is the issue not the rest of the code.
I’m not entirely sure HOW to use delta. And i’ve fiddled with it so so many ways and non seem to work and they either make it too slow, too fast, or don’t affect it and still have a glaring difference between 240 and 60.
local vel 0
local dir = -1
local accel = 1.4 -- Constant. Is
local lastdirchange = tick() -- Is used to determine the last direction change.
local pbar = ui:WaitForChild("PlayerBar") -- Simply just the UI frame that the player controls.
local connect
connect = game:GetService("RunService").RenderStepped:Connect(function(delta)
vel = math.clamp(vel + (dir*accel)*(tick()-lastdirchange), -16, 16) * delta
pbar.Position = UDim2.new(math.clamp(pbar.Position.X.Scale+(vel*0.001), 0+pbar.Size.X.Scale/2, 1-pbar.Size.X.Scale/2), 0, 0.5, 0)
end)
Delta Time is the time that it took between the last and current frame render.
Sorry if I got the code wrong, I’m not entirely sure how to correctly use delta time as well haha.
I’ve tried this and this slows it down so drastically its as if its not moving. Thank you though! I’ve really been struggling with this and im not sure what the issue is!
local vel 0
local dir = -1
local accel = 1.4 -- Constant. Is
local lastdirchange = tick() -- Is used to determine the last direction change.
local pbar = ui:WaitForChild("PlayerBar") -- Simply just the UI frame that the player controls.
local connect
connect = game:GetService("RunService").RenderStepped:Connect(function(delta)
vel = math.clamp(vel + (dir*accel)*(tick()-lastdirchange), -16, 16) * delta * 60
pbar.Position = UDim2.new(math.clamp(pbar.Position.X.Scale+(vel*0.001), 0+pbar.Size.X.Scale/2, 1-pbar.Size.X.Scale/2), 0, 0.5, 0)
end)
local vel = 0
local dir = -1
local accel = 1.4 -- Constant. Is
local lastdirchange = tick() -- Is used to determine the last direction change.
local pbar = ui:WaitForChild("PlayerBar") -- Simply just the UI frame that the player controls.
local connect
connect = game:GetService("RunService").RenderStepped:Connect(function(delta)
-- Update velocity based on acceleration and time delta
vel = vel + (dir * accel * delta)
-- Clamp the velocity to ensure it stays within bounds
vel = math.clamp(vel, -16, 16)
-- Update the position based on velocity and time delta
local newPosX = pbar.Position.X.Scale + (vel * 0.001 * delta)
newPosX = math.clamp(newPosX, 0 + pbar.Size.X.Scale / 2, 1 - pbar.Size.X.Scale / 2)
-- Apply the new position
pbar.Position = UDim2.new(newPosX, 0, 0.5, 0)
end)
I’m not really the best at delta time as I’ve said before, so this is my only way of helping you here.
It doesn’t look like FPS should be having an impact because a benchmark is already being used. tick() is deprecated though, so you’ll want to replace it with os.clock().