I'm making a spring with raycasts but the spring drifts
local RunService = game:GetService("RunService")
local spring = script.Parent
local springLength = 6
local restLength = 5
local stiffness = 400
local damping = 10
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {spring}
RunService.Heartbeat:Connect(function(dt)
local raycast = workspace:Raycast(spring.Position, -spring.CFrame.UpVector * springLength, raycastParams)
if raycast then
local hit = (spring.Position - raycast.Position).Magnitude
local velocity = spring.AssemblyLinearVelocity
local relVel = velocity:Dot(spring.CFrame.UpVector)
local force = stiffness * (restLength - hit)
local dampForce = -damping * relVel
spring:ApplyImpulse(spring.CFrame.UpVector * (force + dampForce) * dt)
end
end)
** You are now Level 1! **