Vertical ladder in 2D game

0

Let's see I'm designing a 2D game with unity, but I'm looking for a solution to make a method in my main character's script where when entering a collider2D (with a Trigger2D) placed in his vertical stair sprite, he can activate your climb animation and can move upwards, canceling gravity and leaving the collider to activate it again

    
asked by Miguel 02.12.2018 в 16:28
source

1 answer

0

After investigating and with the help of a teacher we managed to get the character up the stairs and move on it:

When entering and leaving the stairs:

private void OnTriggerEnter2D(Collider2D collider2D)
{
    if (collider2D.tag == "Player")
    {
        collider2D.GetComponent<PlayerController>().MoveStair();
    }
}

private void OnTriggerExit2D(Collider2D collider2D)
{
    if (collider2D.tag == "Player")
    {
        collider2D.GetComponent<PlayerController>().ExitStair();
    }
}

In the character controller:

[SerializeField]
private bool _isClimbing = false;

public void MoveStair()
{
    _isClimbing = true;
    _rigidBody2D.gravityScale = 0f;
    _animator.SetTrigger("Climb");
    Debug.Log("Entro en una escalera");
}

public void ExitStair()
{
    _isClimbing = false;
    _rigidBody2D.gravityScale = 1f;
    _animator.SetTrigger("ExitClimb");
    Debug.Log("Salio de una escalera");
}

Note: when we finished climbing the ladder the character did not manage to get down, for a boxcollider theme (using 2D platform effector).

    
answered by 09.12.2018 в 01:46