I’m trying to make moving platforms that player can jump between, but it’s glitching the player when they jump/land
local LastPlatformCFrame
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
function MoveComponent:HeartbeatUpdate()
local Character = Player.Character
if not Character then
return
end
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
if not HumanoidRootPart then
return
end
Params.FilterDescendantsInstances = { Character }
local Raycast = workspace:Raycast(HumanoidRootPart.CFrame.Position, Vector3.new(0, -3, 0), Params)
if not Raycast then
return
end
local Hit = Raycast.Instance
if Hit and Hit.Name == "Platform" then
if LastPlatformCFrame == nil then
LastPlatformCFrame = Hit.CFrame
end
local Relative = Hit.CFrame * LastPlatformCFrame:Inverse()
LastPlatformCFrame = Hit.CFrame
HumanoidRootPart.CFrame = Relative * HumanoidRootPart.CFrame
else
LastPlatformCFrame = nil
end
end
I’m not sure what’s going on. Here are some things to consider:
Raycasting 3 downwards from the HRP is exactly the length of the hip height + the offset from the HRP. It might be important to have a slight tolerance (e.g. 3.05).
It’s probably better to reset the LastPlatformCFrame when the Hit changes (in addition to when there is no Hit). This could be done by comparing the current hit with a “LastHit” variable.
Make sure your code is run from the client.
Another note is that your code doesn’t stick the player if their center is off the platform. It might be good to add a few more raycasts or use a shape cast in the future.
Edit:
The problem is probably that LastPlatformCFrame isn’t being reset when the Raycast doesn’t hit anything (the if not Raycast then return end). I think this causes the code to use the difference between the last platform and the new platform and apply that to the character.
So to fix this just add a LastPlatformCFrame = nil before returning there.