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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Getting the First Person Controller (Unity 5 Version) to slide down super steep slopes

Discussion in 'Scripting' started by JustJeff, Jun 23, 2015.

  1. JustJeff

    JustJeff

    Joined:
    Feb 16, 2015
    Posts:
    84
    Out of the box, it seems (at least how I have it set up) that you can bunny hop up a nearly 90 degree terrain slope bit by bit and get to the top, and I need it so they slide down if they're on an angle of a given steepness (say, > 60 degrees or something). The slope limit, of course, is just for what they can walk/run up and I need them caged into the terrain.

    I've tried messing with applying downward force to the rigidbody and with using moveposition hoping the collider would just slide it down as it depenetrated the collider from the terrain, but to no avail.


    I'm sure some of you have done this before, so how did you approach it most successfully? Any code to share would be especially helpful too. Thanks :)
     
  2. JustJeff

    JustJeff

    Joined:
    Feb 16, 2015
    Posts:
    84
    Bump because this needs an explicit answer based on all the similar questions I read searching the fora and asnwers... :)
     
  3. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    233
    if you do a raycast downwards, you can use the hit.normal to find the angle of the slope he's on.

    Code (CSharp):
    1. RaycastHit hit;
    2.      
    3. if (Physics.Raycast(transform.position, -Vector3.up, out hit, 1)){
    4.     Debug.Log(hit.normal);
    5. }
    Ex: hit.normal = (0.5, 0, 0.5)
    If it looks like this ^, then his x and z are on a slope that is angled 50%. You can use the x and z to determine how steep the slope is, and apply force in the opposite direction if you want (so he moves away+down the slope)
     
  4. JustJeff

    JustJeff

    Joined:
    Feb 16, 2015
    Posts:
    84
    Ooooh perfect! I'd been doing something very roundabout to get the angle and that looks perfect... thanks!

    I was playing Skyrim today and I actually noticed that instead of forcing a player down a slope, they prevent hopping up steep mountains by simply not letting you jump if you're on too steep a slope so I'm going to implement this and then see if adding a force benefits as well.

    Thanks :)
     
    Juice-Tin likes this.