Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Help Please]How to make a 2D stairs you can walk by

Discussion in '2D' started by LazyDog_Rich, Oct 4, 2017.

  1. LazyDog_Rich

    LazyDog_Rich

    Joined:
    Apr 27, 2014
    Posts:
    70
    Hi, I want to make a 2D stair, that can be walk through, and only walk over it if you "land" on it. I mean if you fall on it, or if you are next to it and just jump and land on it.

    Is a regular kind of stairs of 2D But I dont know how to achive this.

    I try using Plataform Effector but this dosent work

    Here's a image of what I want to achive




    There's a lot of 2D using this stairs, now I only can think on room13
     
  2. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    What happened when you tried to use platform effector exactly?
     
  3. LazyDog_Rich

    LazyDog_Rich

    Joined:
    Apr 27, 2014
    Posts:
    70
    I try rotating the Platforming, to match the stair collider inclination, and change a lot of values and test. If I can make the player go through the stairs, then I cannot land on them anymore. If I can walk over the stairs, the player cannot walk through them.
    I tried a lot of different combinations but nothing worked :/
     
  4. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    Searching your issue with google, it seems poeple use two colliders to acheive nicely this kind of things. They add a collider set to trigger under the stairs . They make the player ignore the collision with the stairs wile they touch the trigger under it. When the player jumps, once he is above the stairs, they unignore the collisions between the player and the stairs.

    I'm not sure it is the best way to go but it might work.
     
  5. LazyDog_Rich

    LazyDog_Rich

    Joined:
    Apr 27, 2014
    Posts:
    70
    Thanks for that info. I make a search on google but I found nothing, maybe I used the wrong keywords :/

    You mean something like this right:


    I can see that working, a small trigger that can only be touching if is walking, that make player ignore collision with stairs.

    I gonna try it, thanks!
     

    Attached Files:

  6. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    Actually, that wasn't what I meant, but your way is better than mine.
     
  7. DanySkk

    DanySkk

    Joined:
    Jan 13, 2019
    Posts:
    6
    Years later, I'm going to post the solution that worked for me:

    Code (CSharp):
    1.     List<GameObject> grounds;
    2.     List<GameObject> stairs;
    3.  
    4.     public bool isDown;
    5.     public bool isUp;
    6.     public bool isStair;
    7.     public bool isGround;
    8.  
    9.     private void Start()
    10.     {
    11.         grounds = PlayersData.Instance.grounds;
    12.         stairs = PlayersData.Instance.stairs;
    13.     }
    14.  
    15.     private void OnTriggerStay2D(Collider2D other)
    16.     {
    17.         //To Down
    18.         if (other.gameObject.layer == LayerMask.NameToLayer("Player_1") || other.gameObject.layer == LayerMask.NameToLayer("Player_2"))
    19.         {
    20.             if (other.GetComponent<PlayerMovement>().toDown && isDown)
    21.             {
    22.                 for (int i = 0; i < grounds.Count; i++)
    23.                 {
    24.                     Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), grounds[i].GetComponent<CompositeCollider2D>(), true);
    25.                 }
    26.                 for (int i = 0; i < stairs.Count; i++)
    27.                 {
    28.                     Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), stairs[i].GetComponent<CompositeCollider2D>(), false);
    29.                 }
    30.             }
    31.  
    32.             if (!other.GetComponent<PlayerMovement>().toDown && isDown)
    33.             {
    34.                 for (int i = 0; i < grounds.Count; i++)
    35.                 {
    36.                     Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), grounds[i].GetComponent<CompositeCollider2D>(), false);
    37.                 }
    38.                 for (int i = 0; i < stairs.Count; i++)
    39.                 {
    40.                     Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), stairs[i].GetComponent<CompositeCollider2D>(), true);
    41.                 }
    42.             }
    43.  
    44.             //To Up
    45.  
    46.  
    47.             if (other.GetComponent<PlayerMovement>().toUp && isUp)
    48.             {
    49.                 for (int i = 0; i < grounds.Count; i++)
    50.                 {
    51.                     Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), grounds[i].GetComponent<CompositeCollider2D>(), true);
    52.                 }
    53.                 for (int i = 0; i < stairs.Count; i++)
    54.                 {
    55.                     Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), stairs[i].GetComponent<CompositeCollider2D>(), false);
    56.                 }
    57.             }
    58.  
    59.             if (!other.GetComponent<PlayerMovement>().toUp && isUp)
    60.             {
    61.                 for (int i = 0; i < grounds.Count; i++)
    62.                 {
    63.                     Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), grounds[i].GetComponent<CompositeCollider2D>(), false);
    64.                 }
    65.                 for (int i = 0; i < stairs.Count; i++)
    66.                 {
    67.                     Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), stairs[i].GetComponent<CompositeCollider2D>(), true);
    68.                 }
    69.             }
    70.  
    71.  
    72.             //If Flat (Cuando no hay opción entre escaleras y ground)
    73.  
    74.             if (isStair)
    75.             {
    76.                 for (int i = 0; i < grounds.Count; i++)
    77.                 {
    78.                     Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), grounds[i].GetComponent<CompositeCollider2D>(), true);
    79.                 }
    80.                 for (int i = 0; i < stairs.Count; i++)
    81.                 {
    82.                     Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), stairs[i].GetComponent<CompositeCollider2D>(), false);
    83.                 }
    84.             }
    85.  
    86.             if (isGround)
    87.             {
    88.                 for (int i = 0; i < grounds.Count; i++)
    89.                 {
    90.                     Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), grounds[i].GetComponent<CompositeCollider2D>(), false);
    91.                 }
    92.                 for (int i = 0; i < stairs.Count; i++)
    93.                 {
    94.                     Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), stairs[i].GetComponent<CompositeCollider2D>(), true);
    95.                 }
    96.             }
    97.         }
    98.     }
    99. }
    Basically the solution is to put triggers with booleans indicating which kind of terrain is in. There are two cases:

    1) When there are two options. Go up or down the stair / go straight ahead.

    imagen_2021-02-25_131408.png
    2) When there are no options. Go straight up a stair.
    imagen_2021-02-25_131607.png

    I hope you find it useful.
     
    Konigstiger81 likes this.