Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Climbing 2D Stairs Problem

Discussion in 'Physics' started by 1ht1baron, May 13, 2018.

  1. 1ht1baron

    1ht1baron

    Joined:
    Aug 15, 2017
    Posts:
    65


    My player cannot climb this stairs. Firstly, i want player can climb steep wall. Also, when i stop in step, I do not want player to slide down. Please, help.
     
  2. Elveskevtar

    Elveskevtar

    Joined:
    May 16, 2018
    Posts:
    7
    I vaguely understand your question but you did not explain it well. If you want something of this nature, I believe your best bet is to check if the projected movement of the player is going to collide with a wall. If it is then check if this movement plus some verticalOffset (depending on how high you want your character to be able to climb) will still result in a collision. If it does, you shouldn't do anything and just let your player collide. If it doesn't, find where the vertical step is and move your player up to the step.
     
  3. 1ht1baron

    1ht1baron

    Joined:
    Aug 15, 2017
    Posts:
    65
    Thank you for reply. I solved the problem with FreezePositions.
    Code (CSharp):
    1.    
    2. private Rigidbody2D myRigidbody2D;
    3. void OnTriggerStay2D (Collider2D other)
    4.     {
    5.         if (other.CompareTag("Stairs")) {
    6.          
    7.             if (moveVelocity == 0) {
    8.                 myRigidbody2D.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezeRotation;
    9.  
    10.             } else {
    11.                 myRigidbody2D.constraints = RigidbodyConstraints2D.None;
    12.                 myRigidbody2D.constraints = RigidbodyConstraints2D.FreezeRotation;
    13.  
    14.             }        }
    15.     }