Making a player slide down a slope

I have implemented a way to check if my player is on a slope, and I can now restrict his movement when walking up.
I was however hoping to make him slide down in case a hill was too steep, but I just can’t seem to understand how.

I’m currently moving my player with transform.position.

Any idea how this can be done? I just have absolutely no idea where to start with this.

It’s a variation of wall sliding in collision detection algorithms. Or if you just want a quick & dirty way, just cast a ray downwards and glue the player to where the ray hits.

I’m assuming you’re trying to get rid of that bouncing effect when you’re trying to move sideways and you end up in the air and “fall” down to the slope again?

No I already have a way to clamp my player on slopes.

I found a solution in the end, it was really simple. Gonna post my code here.

    private void SlopeDirection()
    {
        // Raycast with infinite distance to check the slope directly under the player no matter where they are
        Physics.Raycast (this.transform.position, Vector3.down, out hit, Mathf.Infinity);

        // Saving the normal
        Vector3 n = hit.normal;
        
        // Crossing my normal with the player's up vector (if your player rotates I guess you can just use Vector3.up to create a vector parallel to the ground
        Vector3 groundParallel = Vector3.Cross ( transform.up, n);

        // Crossing the vector we made before with the initial normal gives us a vector that is parallel to the slope and always pointing down
        slopeParallel = Vector3.Cross ( groundParallel, n);
        Debug.DrawRay (hit.point, slopeParallel * 10, Color.green);

        // Just the current angle we're standing on
        float currentSlope = Mathf.Round (Vector3.Angle (hit.normal, transform.up));
        Debug.Log (currentSlope);

        // If the slope is on a slope too steep and the player is Grounded the player is pushed down the slope.
        if (currentSlope >= 45f && MaintainingGround()) {
            isSliding = true;
            transform.position += slopeParallel.normalized / 2;
        }

        // If the player is standing on a slope that isn't too steep, is grounded, as is not sliding anymore we start a function to count time
        else if (currentSlope < 45 && MaintainingGround() && isSliding) {
            TimePassed ();
                
            // If enough time has passed the sliding stops. There's no need for these last two if statements, the thing works already, but it's nicer to have the player slide for a little bit more once they get back on the ground
            if (currentSlope < 45 && MaintainingGround() && isSliding && timePassed > 1f) {
                isSliding = false;
            }
        }
    }

It’s very dirty and needs some more work but it works nicely

6 Likes

Can you show the full script please

This thread is four years old. Please don’t reply to old threads.

Scripts alone are nothing without properly being connected and set up in the scene. You will have far more luck pursuing this through the usual Youtube tutorial searching:

7675915--959164--Screen Shot 2021-11-21 at 12.05.05 PM.jpg