Help with slide mechanic, slopes not being detected correctly

For my slide mechanic, I wanted players to have the choice to slide down slopes, but I’ve been having
trouble correctly detecting a slope that travels down. My code so far is this:

local slope = false
local params = RaycastParams.new()
params.FilterDescendantsInstances = {workspace.Living}
params.FilterType = Enum.RaycastFilterType.Blacklist

local dist = 5
local results = workspace:Raycast(root.CFrame.p, -root.CFrame.upVector * dist, params)
if results then
    local normal = results.Normal
    local incline = Vector3.new(1, 0, 0):Cross(normal)
    if incline.Magnitude == 0 then
    incline = Vector3.new(1, 0, 0)
end
print(incline)

local angle = math.acos(Vector3.new(0, 1, 0):Dot(incline))
if angle > math.pi / 1.5 then
    slope = true
end

if slope then
    -- Slide Down Slope
    print("Yes")
else
    -- Slide For Short Distance Foward
    print("No")
    end
end
2 Likes

Just do

local angle = math.acos(results.Normal.Y)

No need for all the crosses and stuff.

2 Likes

It’s still printing no for me while i’m ontop a slope. Would I have to change:

if angle > math.pi / 1.5 then
    slope = true
end

Well, math.pi / 1.5 is 120 degrees. Would be hard to have a slope that’s 120 degrees steep!

The angle will be 0 degrees on flat ground, and 90 degrees for straight up and down cliffs.

You can use angle > math.rad(45) for example to use degrees instead.

Couldn’t you just to this instead?

local dist = 5
local SLIP_ANGLE = 45
local SLIP_COS = math.cos(math.rad(SLIP_ANGLE))
local RaycastResult = workspace:Raycast(root.CFrame.p, -root.CFrame.upVector * dist, params) -- Raycast Code Here

if RaycastResult.Normal.Y < SLIP_COS  then
   print("Yes")
else
    print("No")
end

Wouldn’t this work? I dunno about if we need math.rad

Assuming you are not upside down…

1 Like

how do i implement this in a script for sliding + uis