Adding a sliding mechanic

I want to add a sliding mechanic to my current movement script I’m not quite sure how though, because I want the sliding to increase and decrease depending on the steepness of what you’re standing on. Also I think you have to use something like this code but I’m not quite sure how I should implement it into my movement script:

private IEnumerator SmoothlyLerpMoveSpeed()
{
    // smoothly lerp movementSpeed to desired value
    float time = 0;
    float difference = Mathf.Abs(desiredMoveSpeed - moveSpeed);
    float startValue = moveSpeed;

    while (time < difference)
    {
        moveSpeed = Mathf.Lerp(startValue, desiredMoveSpeed, time / difference);

        if (OnSlope())
        {
            float slopeAngle = Vector3.Angle(Vector3.up, slopeHit.normal);
            float slopeAngleIncrease = 1 + (slopeAngle / 90f);

            time += Time.deltaTime * speedIncreaseMultiplier * slopeIncreaseMultiplier * slopeAngleIncrease;
        }
        else
        {
            time += Time.deltaTime * speedIncreaseMultiplier;
        }

        yield return null;
    }

    moveSpeed = desiredMoveSpeed;
}

My current movement script as of writing this:

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

public class PlayerMovement : MonoBehaviour
{
    [Header("Movement")]
    private float moveSpeed;
    public float walkSpeed;
    public float sprintSpeed;

    public float groundDrag;

    [Header("Jumping")]
    public float jumpForce;
    public float jumpCooldown;
    public float airMultiplier;
    bool readyToJump;

    [Header("Crouching")]
    public float crouchSpeed;
    public float crouchYScale;
    private float startYScale;

    [Header("Keybinds")]
    public KeyCode jumpKey = KeyCode.Space;
    public KeyCode sprintKey = KeyCode.LeftShift;
    public KeyCode crouchKey = KeyCode.LeftControl;

    [Header("Ground Check")]
    public float playerHeight;
    public LayerMask whatIsGround;
    bool grounded;

    public Transform orientation;

    float horizontalInput;
    float verticalInput;

    Vector3 moveDirection;

    Rigidbody rb;

    public MovementState state;

    public enum MovementState
    {
        walking,
        sprinting,
        crouching,
        air
    }

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.freezeRotation = true;

        readyToJump = true;

        startYScale = transform.localScale.y;
    }

    private void Update()
    {
        // ground check
        grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.3f, whatIsGround);

        MyInput();
        SpeedControl();
        StateHandler();

        // handle drag
        if (grounded)
            rb.drag = groundDrag;
        else
            rb.drag = 0;
    }

    private void FixedUpdate()
    {
        MovePlayer();
    }

    private void MyInput()
    {
        horizontalInput = Input.GetAxisRaw("Horizontal");
        verticalInput = Input.GetAxisRaw("Vertical");

        // when to jump
        if (Input.GetKeyDown(jumpKey) && readyToJump && grounded)
        {
            readyToJump = false;

            Jump();

            Invoke(nameof(ResetJump), jumpCooldown);
        }

        //start crouch
        if (Input.GetKeyDown(crouchKey))
        {
            transform.localScale = new Vector3(transform.localScale.x, crouchYScale, transform.localScale.z);
        }

        //stop crouch
        if (Input.GetKeyUp(crouchKey))
        {
            transform.localScale = new Vector3(transform.localScale.x, startYScale, transform.localScale.z);
        }
    }

    private void StateHandler()
    {
        if (Input.GetKey(crouchKey))
        {
            state = MovementState.crouching;
            moveSpeed = crouchSpeed;
        }
        // Mode - Sprinting
        else if (grounded && Input.GetKey(sprintKey))
        {
            state = MovementState.sprinting;
            moveSpeed = sprintSpeed;
        }
        else if (grounded)
        {
            state = MovementState.walking;
            moveSpeed = walkSpeed;
        }
        else
        {
            state = MovementState.air;
        }
    }

    private void MovePlayer()
    {
        // calculate movement direction
        moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;

        // on ground
        if (grounded)
            rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);

        // in air
        else if (!grounded)
            rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
    }

    private void SpeedControl()
    {
        Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

        // limit velocity if needed
        if (flatVel.magnitude > moveSpeed)
        {
            Vector3 limitedVel = flatVel.normalized * moveSpeed;
            rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
        }
    }

    private void Jump()
    {
        // reset y velocity
        rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

        rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    }
    private void ResetJump()
    {
        readyToJump = true;
    }
}

Do you mean sliding on command, like power-sliding? The character controller I linked to you two days ago contains a powerslide… go look at it, the same ideas related to CharacterController will apply to Rigidbody.

Here’s the link again if you lost it:

If instead you mean “slide downhill on steep hills,” just use the normal of the contact surface below you (any of the physics casts can give you this) and if it is too steep, offset the player downhill.

Sorry, I meant sliding speed (I want the sliding speed to increase depending on the steepness of what your standing on for an example a slope).

Detect a slope by projecting the normal onto a flat plane with:

Vector3.ProjectOnPlane()

Study the magnitude of that flattenend normal to decide if you want to slide or not.

// do we slide?
if (flattenedNormal.magnitude > 0.2f)
{
  // yes we slide!

  Vector3 slideMotion = flattenedNormal * SlidingSpeed;

  // use slideMotion to offset your player downhill
}

you can also get the dot product of the world up vector and the slope’s normal and it will get the amount of slope difference which you can multiply by your slide speed.

you most likely will want to clamp it to 0-1 (it returns a value from -1 to 1), and you may want to negate it before you clamp if it’s in reverse from what you expect.