Inertia (Keep velocity when jumping off of a moving part)

Very quick script I threw together to replicate inertia properly in Roblox. In games like sharkbite, I personally get annoyed when you jump and whatever platform you are standing on moves without you. This feels very buggy and unintended, and there is a super easy script to fix it.

LocalScript belonging in StarterCharacterScripts:

local Character = script.Parent;
local Human = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")

local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.FilterDescendantsInstances = {Character}

local function GetFloor()
	local Origin = Root.Position
	local Direction = Vector3.new(0, -3, 0)
	local Result = workspace:Raycast(Origin, Direction, Params)
	if Result then
		return Result.Instance
	else
		return nil
	end
end

local function RidInertia()
	local Inertia = Root:FindFirstChild("Inertia")
	if Inertia then Inertia:Destroy() end
end

Human.StateChanged:Connect(function(old, new)
	if new == Enum.HumanoidStateType.Landed then
		RidInertia()
	elseif new == Enum.HumanoidStateType.Jumping then
		local Floor = GetFloor()
		if Floor and Floor.Velocity.Magnitude > 0 then
			RidInertia()
			local Velocity = Instance.new("BodyVelocity", Root)
			Velocity.Name = "Inertia"
			Velocity.MaxForce = Vector3.new(math.huge, 0, math.huge)
			Velocity.P = Floor.Velocity.Magnitude
			Velocity.Velocity = Floor.Velocity + Root.Velocity
		end
	end
end)

Doesn’t work with the Y velocity, but I can’t think of many practical scenarios for that usage.

Hope you like it. :+1:

16 Likes