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

How to make Rigidbody2D to climb on rectangular stairs?

Discussion in '2D' started by Epsiloncool, Mar 30, 2016.

  1. Epsiloncool

    Epsiloncool

    Joined:
    Mar 30, 2016
    Posts:
    16
    Well, I am developing small game based on Rigidbody2D character. He should go horizontally, jump and automatically climb on rectangular stairs when walking on them. I know slopes is a possible solution, but I have complex maps and it is just not possible to make another layer with collider mesh. Instead, I am putting BoxCollider2D above each tile placed on game world. All tiles are rectangular.
    Please check how this type of climbing works on Dizzy game:
    time 0:47 and 1:05 for example. The character can walk onto relatively low steps and they are pushing him up.
    This is what I want to achieve with Rigidbody2D. Is it possible? Who did solve the pbm like this? Thanks!
     
  2. IsaiahKelly

    IsaiahKelly

    Joined:
    Nov 11, 2012
    Posts:
    418
    One way to do this would be to use Physics2D.OverlapArea or Physics2D.BoxCast to detect colliders in front of the character (at the feet and midsection). Then, if you find a collider at the feet, but the midsection is free, you just move the character up in the air and maybe forward a little to get over the step. This would, of course, also need to account for both left and right movement directions.

    Take a look at the 2D platformer example content in the Standard Assets for ideas on how to use 2D casting like this:
    Code (CSharp):
    1.             // If crouching, check to see if the character can stand up
    2.             if (!crouch && m_Anim.GetBool("Crouch"))
    3.             {
    4.                 // If the character has a ceiling preventing them from standing up, keep them crouching
    5.                 if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
    6.                 {
    7.                     crouch = true;
    8.                 }
    9.             }
     
    Last edited: Mar 31, 2016
    Epsiloncool likes this.
  3. Epsiloncool

    Epsiloncool

    Joined:
    Mar 30, 2016
    Posts:
    16
    Thanks for pointed me to these cool methods, Isaiah! I will try now and tell whether it works for me.
     
  4. Epsiloncool

    Epsiloncool

    Joined:
    Mar 30, 2016
    Posts:
    16
    I am back to say "Big thank you", the method proposed by you is working just awesome!
     
    IsaiahKelly likes this.
  5. IsaiahKelly

    IsaiahKelly

    Joined:
    Nov 11, 2012
    Posts:
    418
    That's great. Glad I could help.