I’m trying to simulate marble physics using WASD. Right now, I am using :ApplyImpulse() and the Humanoid.MoveDirection every frame in order to move the marble. However, I suspect that doing this is the reason I freeze every couple of seconds. The video provided showcases the issue.
Here is the code:
local function _onCharacterAdded(character: Model)
local humanoid = character:WaitForChild("Humanoid", 5) :: Humanoid
humanoid.PlatformStand = true
local hrp = character:WaitForChild("HumanoidRootPart", 5)
local MarbleTemplate = workspace:WaitForChild("Marble")
if not humanoid or not hrp then
return
end
local cloneMarble = MarbleTemplate:Clone() :: BasePart
cloneMarble.Parent = workspace
cloneMarble.Name = Players:GetPlayerFromCharacter(character).Name
cloneMarble.CFrame = hrp.CFrame
local weld = Instance.new("WeldConstraint")
weld.Part0 = cloneMarble
weld.Part1 = hrp
weld.Parent = cloneMarble
local lastMoveDirection = Vector3.zero
RunService.Heartbeat:Connect(function(dt) -- this is where the trouble starts
local velocity = cloneMarble.AssemblyLinearVelocity
local horizontalVelocity = Vector3.new(velocity.X, 0, velocity.Z)
local speed = horizontalVelocity.Magnitude
local moveDirection = humanoid.MoveDirection
local t = math.clamp(1 - speed / MaxSpeed, 0, 1)
if moveDirection.Magnitude == 0 then
lastMoveDirection = moveDirection.Unit
end
if lastMoveDirection.Magnitude == 0 then
return
end
local impulse = moveDirection * Acceleration * dt * cloneMarble.AssemblyMass * t
cloneMarble:ApplyImpluse(impulse)
end)
end
Help is appreciated!