Search Unity

How to Get a Rigidbody Player to walk on any type of stairs ?

Discussion in 'Scripting' started by wolfstien786, Apr 25, 2012.

Thread Status:
Not open for further replies.
  1. wolfstien786

    wolfstien786

    Joined:
    Apr 12, 2012
    Posts:
    185
    How would I get my rigidbody player to walk on stairs of any height and length. I tried using an invisible ramp over my stairs but the result was pretty unrealistic and the player would fly off the ramp. Please help



    [EDIT] : I solved it :D or at least it works for me....... Since I was checking if the player was grounded by comparing the current slope of the ground with the maxSlope variable and setting grounded (another variable) to true or false as necessary .... All I had to do then was to make sure that the current slope of the ground was greater than the maxSlope variable (Since stairs are always stacked upon each other at 90 degrees) and also that the tag of the ground was "Stairs". If so then the grounded variable would be set to true and the player then happily walks up the stairs :eek:)
     
    Last edited: Jun 26, 2012
  2. Zethariel1

    Zethariel1

    Joined:
    Mar 21, 2012
    Posts:
    439
    Use a Character Controller instead.
     
    Fairyessfeli and Kinn like this.
  3. Catsoft-Studios

    Catsoft-Studios

    Joined:
    Jan 15, 2011
    Posts:
    703
    As you pointed out, the correct way of doing it it using an invisible ramp (or at least, most of the games I've seen use this trick). It should not be a problem if you don't have an exaggerated ramp inclination or gravity is not proportional to the player's size (remember it should be around 2 UnityUnits.

    If the problem persists, you could put the ramp into a new layer (let's say 8) and every time the player raycasts down and sees a ramp, apply some vertical force downwards to make sure he does not fly off. It's not a very elegant solution though...
     
  4. wolfstien786

    wolfstien786

    Joined:
    Apr 12, 2012
    Posts:
    185
    Thanks Hasho........ I will probably go with that solution but then again if someone has something else then please do tell me.

    Zethariel thats the very reason I changed to creating players with rigidbodies :p
     
  5. Zethariel1

    Zethariel1

    Joined:
    Mar 21, 2012
    Posts:
    439
    Why? A CharacterController has a variable designed SPECIFICALLY to handle stair movement -- Step Offset
     
    calurin, jake46116 and reshmasuresh like this.
  6. Catsoft-Studios

    Catsoft-Studios

    Joined:
    Jan 15, 2011
    Posts:
    703
    Really? This is the first time I've noticed there was that variable in the CharacterController (maybe because I don't really use it and I always create my own PlayerController). Anyway, good point.
     
  7. wolfstien786

    wolfstien786

    Joined:
    Apr 12, 2012
    Posts:
    185
    Because it does not react realistically to physics (at least thats what I think :p)
     
    nreed132 likes this.
  8. Zethariel1

    Zethariel1

    Joined:
    Mar 21, 2012
    Posts:
    439
    No, it doesn't, true. Modern games try too hard to be realistic IMO, and (at least that is what I need) not phasing through doors is god enough for me :)
     
    goldcloud144 and chad_ghost like this.
  9. Catsoft-Studios

    Catsoft-Studios

    Joined:
    Jan 15, 2011
    Posts:
    703
    By the way, check out the 2D platform game tutorial from lerpz. The controller uses a component to behave like if it was a rigidbody (pushing crates off when the player tries to go through them).
     
  10. wolfstien786

    wolfstien786

    Joined:
    Apr 12, 2012
    Posts:
    185
    Thats done by a script which adds force to any rigidbody that the player collides with. So its not at all realistic (at least for me :p).
     
  11. wolfstien786

    wolfstien786

    Joined:
    Apr 12, 2012
    Posts:
    185
    *Bump* please help
     
  12. wolfstien786

    wolfstien786

    Joined:
    Apr 12, 2012
    Posts:
    185
    *BUMP*.......
     
  13. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    What about the locomotion system here
     
  14. wolfstien786

    wolfstien786

    Joined:
    Apr 12, 2012
    Posts:
    185
    Actually the player is just an invisible rigidbody capsule :p......... So I guess its not going top work :(
     
  15. wolfstien786

    wolfstien786

    Joined:
    Apr 12, 2012
    Posts:
    185
    *Bump*........
     
  16. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    Is your rigid body kinematic, or are you moving it by applying forces?
     
  17. wolfstien786

    wolfstien786

    Joined:
    Apr 12, 2012
    Posts:
    185
    Nope its not kinematic. And yes I am moving it by applying forces
     
  18. wolfstien786

    wolfstien786

    Joined:
    Apr 12, 2012
    Posts:
    185
  19. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    I'm not sure why you're still bumping... you got your answer: put an invisible ramp on it. If you want "realistic", then if you push a big solid capsule down a set of actual stairs, it does pretty much what it's doing in game and what you don't like: it will bounce down the stairs in a really ugly and jittery way. That's real life. If you want it to smoothly "walk" down stairs, it can't be a big solid capsule. You'd need to use an IK system like the one linked in the above post that has separate physics on the actual legs, knees, and feet rather than one big capsule.
     
  20. iuripujol

    iuripujol

    Joined:
    Nov 26, 2013
    Posts:
    10
    I have managed to solve this issue with a line that solves not only any type of stairs but also keeps the character attached to the ground, do not miss out, send me an email if you are interested to know how I did it.

    It only works with rigidbody + capsule collider at the moment.

    ipctbunity@gmail.com
     
  21. Catsoft-Studios

    Catsoft-Studios

    Joined:
    Jan 15, 2011
    Posts:
    703
    Maybe after 5 years he's figured it out ;-D
    I'm just kidding. Would you mind sharing your solution?
     
  22. iuripujol

    iuripujol

    Joined:
    Nov 26, 2013
    Posts:
    10
    Try this script, it is a sample of what I have made so far regarding the step problem...The caracter should look like this. Hope it helps, feel free to modify it and improve it :D

    Check out the script first before doing anything cause it is a full character controller script with movement, jump and crouch just like the sample from unity 3d standard assets, however this one can handle steps and slopes by creating a "suspension effect" on surface.
     

    Attached Files:

  23. erinunity

    erinunity

    Joined:
    May 30, 2017
    Posts:
    1
    try this script

    •using UnityEngine;
    •using System.Collections;

    •public class MoneySystem : MonoBehaviour {
    •//an internal reference to the system itself
    • private static MoneySystem _instance;

    •//current balance
    • public int money;

    •//interval for saving the money to Playerprefs
    • public float saveInterval;

    •//internal variable which uses getters and setters to ensure that the money system is ALWAYS available.
    • private static MoneySystem instance
    • {
    • get
    • {
    •//if the instance is null, first make sure there's not already a gameobject named MoneySystem. If there is, check for the
    •//MoneySystem component and set it as instance, otherwise add the component and set the new one as instance.
    •// If there isn't a gameobject named MoneySystem, make one and add the MoneySystem component.
    •//Lastly, return the instance.
    • if (_instance == null)
    • {
    • if (GameObject.Find("MoneySystem"))
    • {
    GameObject g = GameObject.Find("MoneySystem");
    • if (g.GetComponent<MoneySystem>())
    • {
    • _instance = g.GetComponent<MoneySystem>();
    • }
    • else
    • {
    • _instance = g.AddComponent<MoneySystem>();
    • }
    • }
    • else
    • {
    GameObject g = new GameObject();
    • g.name = "MoneySystem";
    • _instance = g.AddComponent<MoneySystem>();
    • }
    • }

    • return _instance;
    • }


    • set
    • {
    • _instance = value;
    • }
    • }

    • void Start()
    • {
    •//Make sure the Gameobject is named MoneySystem.
    • gameObject.name = "MoneySystem";

    • _instance = this;

    •//load the saved money
    • AddMoney(PlayerPrefs.GetInt("MoneySave", 0));

    •//start the save interval.
    StartCoroutine("SaveMoney");
    • }

    •//while reality exists, save money every saveInterval.
    • public IEnumerator SaveMoney()
    • {
    • while (true)
    • {
    • yield return new WaitForSeconds(saveInterval);
    PlayerPrefs.SetInt("MoneySave", instance.money);
    • }
    • }

    •//Checks if you have enough money to buy item with cost, if you do buy it and return true. Otherwise, return false.
    • public static bool BuyItem(int cost)
    • {
    • if (instance.money - cost >= 0)
    • {
    • instance.money -= cost;
    • return true;
    • }
    • else
    • {
    • return false;
    • }
    • }

    •//Simply return the balance
    • public static int GetMoney()
    • {
    • return instance.money;
    • }

    •//Add some money to the balance.
    • public static void AddMoney(int amount)
    • {
    • instance.money += amount;
    • }
    •}

     
  24. rjakob13

    rjakob13

    Joined:
    Apr 13, 2016
    Posts:
    45
    I just ran into the same problem that my character could not climb any stairs after changing it to a rigidbody controlled by force. The reason is clear. Physics won't make your body lift just because you hit an obstacle in front. In real life we raise a foot and apply force in the up direction while moving forward.
    The solution in the RigidbodyFPSController (standard assets) was surprisingly easy: Roughly following the real life example we just need to find a way to raise the rigidbody at certain circumstances. After testing several methods all I changed was the sphere cast origin in the GroundCheck method. Now the cast is just a little bit shifted in the forward direction (currently I use 0.1 units). That way the height over ground is checked at that distance in front of the character instead of your y-axis. You can think of that as checking at your toes instead of near the heels.
    I tested that with stairs, ramps and obstacles of different heights and am very satisfied with the results.

    The change in detail is:
    Code (CSharp):
    1.         /// sphere cast down just beyond the bottom of the capsule to see if the capsule is colliding round the bottom
    2.         private void GroundCheck()
    3.         {
    4.             m_PreviouslyGrounded = m_IsGrounded;
    5.             RaycastHit hitInfo;
    6.             if (Physics.SphereCast(transform.position + cam.transform.forward * advancedSettings.checkGroundForwardDistance, m_Capsule.radius * (1.0f - advancedSettings.shellOffset), Vector3.down, out hitInfo,
    7.                                    ((m_Capsule.height/2f) - m_Capsule.radius) + advancedSettings.groundCheckDistance, Physics.AllLayers, QueryTriggerInteraction.Ignore))
    8.  
    (here advancedSettings.checkGroundForwardDistance defaults to 0.1f)
     
  25. eliGasious

    eliGasious

    Joined:
    Apr 7, 2015
    Posts:
    16
    I see that this is an old post, but it also seems like ppl are still facing this problem. I came to this thread searching for the same. "How to keep a Rigid body Capsule Collider from sliding down a ramp?"

    If your colliders are flat, like a ramp, then giving your player a physics material can keep it from sliding around.

    My player would go about .33 U (unity units) up the ramp before gravity would pull it back down, after giving my player the Rubber physics material, he climbs all the way up the stairs.

    No Raycast. No code. No Character Controller. No problem.
     
    eneroth3 likes this.
  26. jake46116

    jake46116

    Joined:
    Jun 27, 2018
    Posts:
    4
    You just saved me precious time
     
  27. NerdIt-

    NerdIt-

    Joined:
    Oct 19, 2018
    Posts:
    24
    For those of you who are spectacle of why people don't just use the Character Controller, it may just be because they didn't like the feel of it. It's physics just don't feel 100%. Also as advanced as the Character controller maybe, it can't do everything, and editing the code can be a pain. There are plenty of reasons not to use it. The developer may be making their own character controller for educational purposes, so a pre-made, pre-set character controller isn't particularly helpful. The developer may already have a character controller they like and that works for the situations they need it to. In that case there is no logical point to scrap an entire character controller just because you want a single feature like Step Offset.

    I am by no means saying that the Unity Character Controller is useless or unneeded, I am just saying that not everyone wants/likes it. So instead of accusing people for not liking the same system as you, try finding out why they don't like it.
     
    Eluem likes this.
  28. Deleted User

    Deleted User

    Guest

    May I suggest adding a Physic Material to the stairs? Then set the static friction to 1. It worked for me.
     
  29. Fairyessfeli

    Fairyessfeli

    Joined:
    Aug 30, 2020
    Posts:
    6
    Hey All, been watching this thread for solutions and while I applaud you all for your techniques, Zathariel1 came in with the win for me. Using Unity Tech Free Sci-fi asset, I bake the agent types at .5 radius; 2 height; .4 step height; and a 40 max slope on humanoid for the name. The baked agent size is .25, 2, .4, and 60 respectively. The drop and jump are 0. Then I head to my player controller (not the object/child holding the animator but its parent)—using Game Creator—and scroll to the Character Controller component “On the Inspector”! I set slope limit to 90.79 (I like being different) , step offset to 2 and viola. No need for Mixamo and triggers with conditions and variables and mess!!! Hope that helps.
     
    Catsoft-Studios likes this.
  30. raider13209

    raider13209

    Joined:
    Aug 19, 2020
    Posts:
    5
    the thing is that some people have learnt and understand the rigidbody system more than the chracter controller
     
  31. GamesOfArcadia

    GamesOfArcadia

    Joined:
    Dec 12, 2020
    Posts:
    2
    Hi there. As there's no very clear answer, here's a very easy and clean solution; add a navmesh agent to your player. Just untoggle the auto options and set up the height and slope you want. It works great!
     
  32. captainspaceman

    captainspaceman

    Joined:
    Jul 14, 2017
    Posts:
    42
    rjakob13 had the best idea by far, you all just didn't take the time to read it lol or his implementation was more complicated than it needed to be:

    Code (CSharp):
    1. if (Physics.Raycast(transform.position - new Vector3(0, .5f, 0), player.transform.TransformDirection(new Vector3(0, -1, 1).normalized), .6f))
    2.         {
    3.             rb.AddForce(new Vector3(0, 5, 0));
    4.         }
    This works with a capsule collider and a ramp, it simply casts a ray forward at a 45 degree angle from the base of the capsule to a little bit outside of it and adds a force upwards if it hits something. You may want to add a layermask so not everything causes it. I thought that was clearly the right answer for this. You could even adjust it so it works with actual stair mesh colliders and not just ramps. Then as other people said add the rubber material to your collider so you don't slide back down. I hate character controllers too.
     
  33. Scorpion37s

    Scorpion37s

    Joined:
    May 31, 2021
    Posts:
    9
    "Use something completely different" isnt an answer
     
  34. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Neither is necroing a thread by replying to a post that is 10 years old.

    Please, in the very least, look at the dates of posts you're responding to.
     
    Kurt-Dekker likes this.
  35. RespawnGaming2

    RespawnGaming2

    Joined:
    Apr 14, 2022
    Posts:
    1
    Hi, I am new a coding and my 2d player will not go up slops when there are slops and it will not go down slops. do you guys think you can help?
     
  36. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    You are hijacking and necroing an old thread from 2012. Please, just create your own thread and describe what you've done so far and what things you need help with. If you are asking for scripts then that's not what the forums are for. There are lots of tutorials online for this kind of stuff. Search for "Unity 2D character controller".

    Also, note that it's "slopes" not "slops".
     
Thread Status:
Not open for further replies.