Y
Y
YakutD2019-01-04 21:37:20
2D
YakutD, 2019-01-04 21:37:20

How to make stairs in Unity 2D?

Hello, we're making a simple 2d platformer and we're having trouble with the stairs ._. The bottom line is that it is necessary for the character to be able to climb the stairs to stand on it, and by pressing the down arrow go down. In order to do this, we used PlatformEffector2D at the platform with which the ladder is in contact at its top point, and wrote a script that should change the rotationalOffset when the character touches the collider of this very platform, depending on its current value (0 or 180) and clamped keys (up or down). Here is the code:

private void OnCollisionStay2D(Collision2D col)
    {
        if (col.gameObject.name == "platform_ladder")
        {
            PlatformEffector2D pe = col.gameObject.GetComponent<PlatformEffector2D>();
            if (Input.GetKeyDown(KeyCode.DownArrow) && pe.rotationalOffset == 0)
            {
                pe.rotationalOffset = 180;
            }
            else if (Input.GetKeyDown(KeyCode.UpArrow) && pe.rotationalOffset == 180)
            {
                pe.rotationalOffset = 0;
            }
        }
    }

As a result, the character climbs the stairs, stands on it, but does not react to pressing the down key. If you move away and stepping on it again, then the script is triggered, and the rotationoffset changes. However, then it is impossible to stand on the platform, and the character simply climbs through it back and forth. I give an example of recording from the screen (unfortunately, the video slows down a lot, but the essence of what I wrote on it is visible)
https://drive.google.com/open?id=1ccUi0sqtVxFS1UAK...

Just in case, I will give the code for climbing the stairs, suddenly the error lies in it:
/* CLimbing */
        //если персонаж находится в зоне подъема по лестнице
        raydistance = 0;
        RaycastHit2D hitinfo = Physics2D.Raycast(transform.position, Vector2.up, raydistance, whatisLadder);

        if (hitinfo.collider != null)
        {

            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                isClimbing = true;

            }
            else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.LeftArrow))
            {
                isClimbing = false;
            }

        }
        else
        {
            isClimbing = false;
        }

        anim.SetBool("OnLadder", isClimbing);

        if (isClimbing && hitinfo.collider != null)
        {
            rb.velocity = new Vector2(rb.position.x, Input.GetAxisRaw("Vertical") * speed);
            // rb.gravityScale = 0;
            rb.isKinematic = true;
            anim.SetBool("Ground", true);//Принудительно блокируем анимацию прыжка
        }
        else
        {
            rb.isKinematic = false;
            // rb.gravityScale = 3;
        }

We do not ask you to write the solution code, but we ask you to explain the principle to us and direct us in the right direction, thanks ^^

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
YakutD, 2019-01-05
@YakutD

Well, the problem was solved independently. Our ladder is a complex object from the platform itself, the ladder, and an invisible trigger. The ladder and trigger belong to two different layers, Ladder and Crutch (that's what we decided to call them), and the platform has the same layer as the ground.
We use Physics.IgnoreCollision(true) when the user raycasts down on the Crutch layer (our trigger), when the ray does not detect this layer, or when the user presses the up key, Physics.IgnoreCollision becomes false again. In order to prevent Physics.IgnoreCollision from switching to false early during the descent, and the platform does not push the user with its collider, the size of the trigger collider is equal to half the height of the stairs, and its top point slightly exceeds the location of the platform itself. In order for the user to go up through the platform, we use the PlatformEffector, if suddenly someone did not pay attention.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question