If you look at the video you can see it looks pretty good, HOWEVER when I walk forward you can see the UI sling forward super fast, instead of smoothy moving. I cant seem to fix it, and maybe a fresh pair of eyes can help!
Here is the code:
-- Created/Owned by TableMayo --
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Constants
local UI_OFFSET = Vector3.new(0, -1.5, -3)
local CENTER_OFFSET = Vector3.new(0, -1.5, -3)
local CENTER_SMOOTHNESS = 0.05
local SPRING_REST_LENGTH = 0.5
local SPRING_STIFFNESS = 1000
local SPRING_DAMPING = 25
local ROTATION_SMOOTHNESS = .3
local DRAG_COEFFICIENT = 0.1
local DAMPING_START_DISTANCE = 0.1
local DAMPING_END_DISTANCE = 0.02
local MAX_LENGTH_THRESHOLD = 0.01
-- Objects
local Camera = workspace.CurrentCamera
local Character = Players.LocalPlayer.Character
local Head = Character:WaitForChild("Head")
local HudFramework = ReplicatedStorage:WaitForChild("Packages").HudFramework
local p_health = HudFramework:WaitForChild("p_health")
local p_centerAnchor = HudFramework:WaitForChild("p_centerAnchor")
-- Check if UI_Holder and centerAnchor exist
if p_health and p_centerAnchor then
-- Clone UI_Holder and centerAnchor
local clonedUI_Holder = p_health:Clone()
clonedUI_Holder.Anchored = false
clonedUI_Holder.Parent = Character
local clonedCenterAnchor = p_centerAnchor:Clone()
clonedCenterAnchor.Parent = Character
clonedCenterAnchor.Anchored = true
-- SpringConstraint customization
local uiSpring = Instance.new("SpringConstraint")
uiSpring.FreeLength = SPRING_REST_LENGTH
uiSpring.Stiffness = SPRING_STIFFNESS
uiSpring.Damping = SPRING_DAMPING
uiSpring.Attachment0 = clonedCenterAnchor:FindFirstChild("ca_A")
uiSpring.Attachment1 = clonedUI_Holder:FindFirstChild("uih_A")
uiSpring.LimitsEnabled = true
uiSpring.MaxLength = 0.05
uiSpring.MinLength = 0
uiSpring.Parent = clonedUI_Holder
local centerAlignment = Instance.new("AlignPosition")
centerAlignment.Attachment0 = clonedUI_Holder:FindFirstChild("uih_A")
centerAlignment.Attachment1 = clonedCenterAnchor:FindFirstChild("ca_A")
centerAlignment.MaxForce = 10000
centerAlignment.Responsiveness = 15
centerAlignment.Parent = clonedCenterAnchor
-- Initializations
local lastPosition = clonedUI_Holder.Position
local lastVelocity = Vector3.new()
local lastUpdateTime = tick()
-- Update function
local function updateViewModel(deltaTime)
clonedCenterAnchor.CFrame = Camera.CFrame * CFrame.new(CENTER_OFFSET)
local cameraPosition = Camera.CFrame.Position
local targetRotation = CFrame.new(clonedUI_Holder.Position, cameraPosition)
clonedUI_Holder.CFrame = clonedUI_Holder.CFrame:lerp(targetRotation, ROTATION_SMOOTHNESS)
local currentVelocity = (clonedUI_Holder.Position - lastPosition) / deltaTime
lastPosition = clonedUI_Holder.Position
local dragForce = currentVelocity * -DRAG_COEFFICIENT
clonedUI_Holder.Position = clonedUI_Holder.Position + dragForce * deltaTime
end
-- Render stepped function
local function onRenderStepped()
local currentTime = tick()
local deltaTime = currentTime - lastUpdateTime
updateViewModel(deltaTime)
local displacement = clonedUI_Holder.Position - clonedCenterAnchor.Position
local currentLength = displacement.Magnitude
local dampingFactor = 1.0
local distanceToMaxLength = uiSpring.MaxLength - currentLength
if distanceToMaxLength < DAMPING_START_DISTANCE then
local t = 1.0 - math.clamp(distanceToMaxLength / DAMPING_START_DISTANCE, 0, 1)
dampingFactor = t + (1 - t) * DAMPING_END_DISTANCE / DAMPING_START_DISTANCE
end
-- Adjust the spring length if it's close to the maximum length
local maxLengthThreshold = uiSpring.MaxLength - MAX_LENGTH_THRESHOLD
if currentLength >= maxLengthThreshold then
local scaleFactor = math.min(1, uiSpring.MaxLength / currentLength)
clonedUI_Holder.Position = clonedCenterAnchor.Position + displacement * scaleFactor
end
lastUpdateTime = currentTime
end
-- Connect the RenderStepped event
RunService.RenderStepped:Connect(onRenderStepped)
end