Setting a range for the character controller slope limit?

I have a bool that checks the slope angle and prevents my player from walking on a slope that is too steep, and my player just slides down instead.

However it is causing issues with non-slope objects such as walls, etc, so basically if I’m standing on the edge of an object that is 90 degrees, my player tries to slide when it shouldn’t, but anything less than 90 degrees should be acceptable

public bool isSliding
{
    get
    {
        if (char.isGrounded && Physics.SphereCast(transform.position, .5f, Vector3.down, out slopeHit, 1.5f))
        {
            normalHit = slopeHit.normal;
            return Vector3.Angle(normalHit, Vector3.up) > char.slopeLimit;
        }
        else
        {
            return false;
        }
    }
}

Is there a way for me to just add an additional condition so that if the angle is both greater than the controller.slopeLimit && less than 90f, then sliding should occur? I didn’t think that far ahead so now I’m not quite sure how to alter my code since trying to add the < 90f to my bool gave no results.

You just want to check if your slope is within a particular range of values:

Vector3 normal = slopHit.normal;
float angle = Vector3.Angle(normal, Vector3.up);
return angle > char.slopSlimit && angle < 90f;
2 Likes

Thanks this worked, I just had to change the 90f to != 0f because I got the degrees wrong.