Last active
July 14, 2022 21:15
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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