Skip to content

Instantly share code, notes, and snippets.

@mvyasu
Last active July 14, 2022 21:15
Show Gist options
  • Select an option

  • Save mvyasu/d761f5286a46458c1827c8cb2201b6f6 to your computer and use it in GitHub Desktop.

Select an option

Save mvyasu/d761f5286a46458c1827c8cb2201b6f6 to your computer and use it in GitHub Desktop.
A simple snippet that attaches the local character to moving platforms that move without physics.
local STICKY_PLATFORM_TAG = "StickyPlatform"
local DISTANCE_TO_CHECK_BELOW = 15
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local lastPlatformCFrame = nil
RunService.Heartbeat:Connect(function()
if not Player.Character then
return
end
local characterPivotCFrame = Player.Character:GetPivot()
local platformRaycastParams = RaycastParams.new()
platformRaycastParams.FilterType = Enum.RaycastFilterType.Whitelist
platformRaycastParams.FilterDescendantsInstances = CollectionService:GetTagged(STICKY_PLATFORM_TAG)
local raycastResult = workspace:Raycast(characterPivotCFrame.Position, -Vector3.yAxis*DISTANCE_TO_CHECK_BELOW, platformRaycastParams)
if raycastResult then
local platformCFrame = raycastResult.Instance.CFrame
if lastPlatformCFrame==nil then
lastPlatformCFrame = platformCFrame
end
local platformRelativeCFrame = platformCFrame * lastPlatformCFrame:inverse()
lastPlatformCFrame = platformCFrame
Player.Character:PivotTo(platformRelativeCFrame * characterPivotCFrame)
else
lastPlatformCFrame = nil
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment