How would one go about making a slide mechanic?

So I am working on a game and I want to add a slide mechanic. When a player presses a key, I want the player to slide so the player can slide under a wall or slide down a slope. I know obviously it would involve animations but how would I detect a slope and make it push the player forward when sliding. Any ideas to help push me in the correct direction?

2 Likes

For detecting if the player is on a slope, you can use the dot and cross products. You’d have to cast a ray downwards and if the ray hit, you can get the surface normal of the intersection, then you’d cross it with the global right vector (1, 0, 0) and use the dot product on that vector to get the angle. This should be in a Heartbeat event:

local incline = Vector3.new(1, 0, 0):Cross(normal)

if incline.magnitude == 0 then
    incline = Vector3.new(1, 0, 0)
end

local angle = math.acos(Vector3.new(0, 1, 0):Dot(incline))

if angle > math.pi / 1.5 then
    -- slope
end

May need to mess with the angle to get correct result


For making the player move down the slope, you can get the initial position which is where the player first had contact with the slope and you can make a variable called velocity which is the velocity that the player will travel down the slope. dt, which is delta time (change in time from the last frame) is a parameter returned in the Heartbeat event

local velocity = 60 -- player will travel at 60 studs / s
HumanoidRootPart.CFrame = CFrame.new(initial) + (incline * velocity * dt)
12 Likes

I thought it seemed like it only slid in the one direction, so I checked the incline-
https://gyazo.com/79393fee72b30c889f92e5ce94244c95
every slope returns the same direction, do you have a method that can check the angle of a rotated surface?

Nevermind- I was able to fix it, by replacing your cframe code.

instead of doing the initial + incline&speed,
I simply moved it by the HumanoidRootPart’s current position + normal * speed, allowing for a slide with scaling speed across slopes, and that follows the angle the slope is facing.

local velocity = 60 -- player will travel at 60 studs / s
HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.Position+normal*velocity*dt)

for extra measures, I wanted to make sure that no slope below a certain angle would slide, but yours seems to only function on one direction- so I used math.abs to ensure the normal values were positive, then check the scale of the slope.

local spd = math.abs(normal.X) + math.abs(normal.Z)
if spd > 0.25 then
--slope
end

thus my “spd” variable would measure how tilted it was from a scale of 0-2, combining both horizontal and vertical rotation.

3 Likes

What’s Normal I just copied down everything and like idk anything about slope Normal and stuff like that I just need like an exact tutorial

A normal is the direction in which the face of a 3d object is facing. All 3d objects in roblox studio are made up of “faces”, think of them as the sides of a dice or the surface of a car. they are individual 2 dimensional shapes that make up the shape of a 3d object. Each of these faces have a top and a bottom, the top side is the side that we see in the game.

In the tutorial above, we use raycasting to determine a specific surface below the player (represented by the blue capsule in the image below). It detects the first “face” (red) that the player is colliding with.

image

The Normal is the direction in which that face is pointing (illustrated in blue in the image below). We can use this to determine the slope of a surface the player is standing on, and can be especially useful when the player is standing on complex shapes.

image

4 Likes

it works but whenever I slide I only go 1 direction