Raycasting slope physics causes player to sometimes teleport into the sky

Hello, my script is intended to make players stay on slopes while going downhill. It works well, except it sometimes teleports the player sky high. I think this may be due to a giant invisible CanTouch block I made, but I made sure RespectCanCollide was on in the ray params.

I want to fix this problem because it is going to be a huge issue in my game.

I have tried changing the CanQuery property on the invisible block to false, but it still does this.

Here is the script I currently have.

local RS = game:GetService("RunService")
local player = game.Players.LocalPlayer
local char = player.Character
local hrp = char:WaitForChild("HumanoidRootPart")
local Hum = char:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local deb = false

local params = RaycastParams.new()
params.FilterDescendantsInstances = {player.Character}
params.FilterType = Enum.RaycastFilterType.Exclude
params.RespectCanCollide = true
params.IgnoreWater = true

local function getAngle(normal)
	return math.deg(math.acos(normal:Dot(Vector3.yAxis)))
end

RS.RenderStepped:Connect(function()
	local State = Hum:GetState()
	if Hum.MoveDirection.X > 0 or Hum.MoveDirection.X < 0 then
		if deb == false then
			local raycastResult = workspace:Raycast(hrp.Position, Vector3.new(0,-12,0), params)
			if raycastResult then
				if not hrp:FindFirstChild("SpringVelocity") then
					if (raycastResult.Position.Y < hrp.Position.Y - 4.5) then
						if hrp.Velocity.Y < 0 then
							if getAngle(raycastResult.Normal) > 0 then
								char:MoveTo(Vector3.new(raycastResult.Position.X,raycastResult.Position.Y + 4.5,raycastResult.Position.Z))
							end
						end
					end
				end
			end
		end
	end
end)

Hum.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Jumping or newState == Enum.HumanoidStateType.Freefall then
		deb = true
	else
		deb = false
	end
end)

If anyone could help me figure out this problem, it would be greatly appreciated. Thank you!

2 Likes

My best guess here is that your raycast is accidentally raycasting to that black part (/ the one that goes up into the sky)

A way that you could fix this is by adding to your RaycastParams and instead of blacklisting the character, whitelisting the road parts. Or you could blacklist the parts that might get in the way.

3 Likes

I blacklisted the black part, and it still sends me into the sky. I’ve also tried whitelisting every part but that black part, but it still won’t work. I have no idea how to fix this problem-

3 Likes

Hello, I did a little research and stumbled upon this: Model | Documentation - Roblox Creator Hub. The MoveTo method of a model will move the model vertically until it is no longer obstructed by BaseParts or Terrain. You should try using PivotTo instead, as it will put the model exactly where you want it:

char:PivotTo(CFrame.new(raycastResult.Position.X,raycastResult.Position.Y + 4.5,raycastResult.Position.Z))
3 Likes

Thank you, it works! However, it incorrectly rotates the player to a different angle. Is there any way to fix that?

2 Likes

I would fix that by instead using TranslateBy. With PivotTo, you are not only assigning the position, but also rotation, because you are using CFrame. You could pass in the rotation of the PrimaryPart, but TranslateBy makes it simple; you only need to know the start position and end position. Subtracting the end position by the start position gives you the translation:

endPosition = Vector3.new(raycastResult.Position.X,raycastResult.Position.Y + 4.5,raycastResult.Position.Z)
startPosition = char.PrimaryPart.CFrame.Position

char:TranslateBy(endPosition - startPosition)

(^^^ untested code, in theory it should work, based on documentation I read)

This assumes that you have a PrimaryPart set, which was also recommended when you were originally using MoveTo.

2 Likes

Thank you so much, everything is working now!

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.