Answer the question
In order to leave comments, you need to log in
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;
}
}
}
https://drive.google.com/open?id=1ccUi0sqtVxFS1UAK...
/* 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;
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question