Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

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

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

  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.
     
    Kinn likes this.
  3. Catsoft-Studios

    Catsoft-Studios

    Joined:
    Jan 15, 2011
    Posts:
    553
    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
     
    jake46116 and reshmasuresh like this.
  6. Catsoft-Studios

    Catsoft-Studios

    Joined:
    Jan 15, 2011
    Posts:
    553
    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)
     
  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 :)
     
    chad_ghost likes this.
  9. Catsoft-Studios

    Catsoft-Studios

    Joined:
    Jan 15, 2011
    Posts:
    553
    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

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,215
    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,301
    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:
    553
    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:
    42
    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:
    9
    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