# How can I check if a player is on a slope?

Im trying to make a roll mechanic so that a player rolling down a slope can get a speed boost as they go to down if their rolling.

But Im not sure how Im supposed to check if their on a slope.

You can probably fire a ray down from the players location and check the raycast resultâ€™s normal vector and use that to determine if itâ€™s angled or not.

An explanation of Normal Vectors can be found here:

and the api for RaycastResult are here:
https://developer.roblox.com/en-us/api-reference/datatype/RaycastResult

3 Likes

Yep like @Motofied said.

To add on to get the direction pointing down the slope you can use cross product of the normal vector with the downvector.

2 Likes

How would I know if the player is going higher or lower on a slope though?

Check the difference in the Y axis positional value of their root part?

1 Like

Love that this reply is still being linked to 3 years after I posted it!

@StarJ3M as @Forummer said, you could track the Y-position of the HumanoidRootPart. You can also look at the root partâ€™s velocity (BasePart.Velocity)

2 Likes

Would it be smarter to use a raycast, or track the Y position of the Cframe?

and also, how would I even track the Y position and check if its value went up or down?

So to check if its angled, could I use some sort of function to check if its a decimal, because my Wedgeâ€™s always end up outputing a decimal value.

And after that, how would I check if the players torso.Position.Y is increasing or decreasing?

Developers arenâ€™t on the DevForum to design an algorithm for - try to come up with a solution before asking for one.

Your first question depends on what youâ€™re trying to achieve. The way to get the answer is to understand how youâ€™d implement each solution, and then decide for yourself which one makes the most sense. When solving a problem in computer science you want to be reducing memory usage, algorithm complexity, and execution time, whilst increasing functionality, code readability, and codebase health.

You canâ€™t have everything though, and youâ€™ll need to make trade-offs. The most common trade-offs are improving readability at the expense of increased execution time, and improving execution time at the expense of increased memory usage. These also exist in the inverse.

For your second question: youâ€™d track the change of the y-position by saving it at one point in time, and then checking it at another point in time, and then comparing the difference. If the player was at y-position 50.4, and is now at y-position 43.7, then theyâ€™ve gone down 6.7 studs, and are descending.
How you go about the details of implementing this is specific to your project, and youâ€™ll need to decide that yourself. I would suggest looking at the RunService

2 Likes