V
V
vkvk2021-02-28 12:28:17
C++ / C#
vkvk, 2021-02-28 12:28:17

How to make stairs for android using Unity2D?

Hello! I'm trying to make a ladder, but it doesn't work. Difficulties arise in the fact that this needs to be adapted for android and Input.GetAxis("Vertical") will not help. I added a few variables and added a separate button for going down the stairs, the button appears when the character comes up to the stairs. Added a few bool variables to check and set the required speed of climbing stairs for the character at the right moment, as well as set the required direction. But nothing works, the buttons either do not work when you approach the stairs, or you are pushed down, no matter which button I press now, down or up. Here is the code.
variables

[Header("Climbing")]
    public bool isClimbing = false;
    public bool ClimbingButtonDown = false;
    public GameObject ClimbingButton;
    public bool JumpButtonClimbing = false;
    public float speedClimbing = 0f;


buttons
public void OnJumpButtonDown()
    {
        if (IsGronded == true && !isClimbing)
        {
            rb.velocity = Vector2.up * JumpForce;
            anim.SetInteger("State", 3);
        }

        if (IsGronded == true && isClimbing)
        {
            rb.bodyType = RigidbodyType2D.Dynamic;
        }

        if (isClimbing)
        {
            speedClimbing = 12f;
        }

        JumpButtonClimbing = true;
    }

public void OnClimbingButtonDown()
    {
        ClimbingButtonDown = true;
        if (isClimbing)
            speedClimbing = 12f;
    }

    public void OnClimbingButtonUp()
    {
        if (!isClimbing)
            ClimbingButton.SetActive(false);
    }


stairs
private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Ladder")
        {
            isClimbing = true;
            gameObject.GetComponent<Player>().ClimbingButton.SetActive(true);
            if (JumpButtonClimbing && isClimbing && !ClimbingButton)
                transform.Translate(Vector3.up * speedClimbing * Time.deltaTime * 0.5f);

            if (ClimbingButton && isClimbing && !JumpButtonClimbing)
            {
                speedClimbing = 12f;
                transform.Translate(Vector3.down * speedClimbing * Time.deltaTime * 0.5f);
            }

            rb.bodyType = RigidbodyType2D.Kinematic;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Ladder")
        {
            ClimbingButton.SetActive(false);
            isClimbing = false;
            rb.bodyType = RigidbodyType2D.Dynamic;
        }
    }


Hope somebody can help. I want to make it so that with the help of buttons the player himself can choose when he will stop on the stairs and start, for example, go down or just continue to go up.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Yermolayev, 2017-11-21
@Shlop

<table>
    <tr>
        <td colspan="2"></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td colspan="3" rowspan="2"></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
    </tr>
</table>

Next, you will correct the sizes, border colors, etc. with styles.

Z
zoozag, 2017-11-21
@zoozag

In the last 2 rows of the first cells, clospan and remove the border

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question