Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Setting a range for the character controller slope limit?

Discussion in 'Scripting' started by TheProcessYet_64, Jul 12, 2023.

  1. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    39
    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

    Code (CSharp):
    1. public bool isSliding
    2. {
    3.     get
    4.     {
    5.         if (char.isGrounded && Physics.SphereCast(transform.position, .5f, Vector3.down, out slopeHit, 1.5f))
    6.         {
    7.             normalHit = slopeHit.normal;
    8.             return Vector3.Angle(normalHit, Vector3.up) > char.slopeLimit;
    9.         }
    10.         else
    11.         {
    12.             return false;
    13.         }
    14.     }
    15. }
    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.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    You just want to check if your slope is within a particular range of values:
    Code (CSharp):
    1. Vector3 normal = slopHit.normal;
    2. float angle = Vector3.Angle(normal, Vector3.up);
    3. return angle > char.slopSlimit && angle < 90f;
     
    TheProcessYet_64 and ijmmai like this.
  3. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    39
    Thanks this worked, I just had to change the 90f to != 0f because I got the degrees wrong.