Need help with player sliding down slopes at certain angles

Hey, everyone! I am trying to create a player that can only go up certain slopes and if the slope is too steep they will slide down it and slide at a speed that is higher in relation to the intensity of the slope.

So far all I have is the character moving and can go up certain slope limits using the character controller but I have no idea where to start with the sliding. I have done some research but still do not understand it and could find nothing that really helped.

Below is what I have so far for movement.

Thanks, all in advance for helping!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    public float moveSpeed;
    public CharacterController controller;

    private Vector3 moveDirection;
    public float gravityScale;




    // Use this for initialization
    void Start () {
        controller = GetComponent<CharacterController>();
    }
   
    // Update is called once per frame
    void Update () {
        moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, 0f, Input.GetAxis("Vertical") * moveSpeed);

        moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale);
      
        controller.Move(moveDirection * Time.deltaTime);
    }

   

}

What you’re wanting is some kind of Friction. The friction between your player and the ground should be what prevents your player from sliding down the hill, not some kind of arbitrary angle. If the friction between the player and the ground isn’t high enough, gravity will push the player downhill.

TBH, but not mean, you may want to use the built in physics engine for your game if you’re not sure of the physical properties for things like this. It will not only help you get things working faster, but you’ll get a much deeper understanding of properly working game physics. You won’t get to code the physics, but trust me… if you’re asking about this, then you don’t know how deep that rabbit hole goes. I can speak from experience, you’ll be doing yourself a favor not trying to do it yourself before you’re capable of properly digesting documentation. :wink:

If you haven’t already, I highly recommend some of the 2D physics tutorials they’ve made to get you started, like this one:

Using the built-in physics engine, you can apply different physical materials to things. You would want to tweak the friction of your ground such that the player slides down it at the right angle you want. Quite trivial once you’re use to the engine. :slight_smile:
Good luck! :smile: