I’ve got a 2D 2-way stairs system, and there are two things wrong with it right now.
-
The triggers are a bit broken (not fully implemented)
-
The UI text doesn’t have specific trigger detection.
Here’s what the setup looks like:
Right now, the player can enter either of the two triggers, which enables the polygon collider, and disables the collider of the above floor.
Here’s the code (feel free to use it for your game (even commercial), just don’t licence it (the code))
public Movement playerMovement;
public PolygonCollider2D stairCollider;
public BoxCollider2D floorCollider;
public Canvas privateCanvas;
bool isInTrigger;
void Update () {
if (isInTrigger == true) {
if (Input.GetKeyDown (KeyCode.W) || Input.GetKeyDown (KeyCode.S)) {
stairCollider.enabled = true;
playerMovement.OnStairs = true;
floorCollider.enabled = false;
}
}
else {
stairCollider.enabled = false;
playerMovement.OnStairs = false;
floorCollider.enabled = true;
}
}
void OnTriggerEnter2D () {
if (!stairCollider.enabled) {
privateCanvas.gameObject.SetActive (true);
isInTrigger = true;
}
else {
isInTrigger = false;
}
}
void OnTriggerExit2D () {
privateCanvas.gameObject.SetActive (false);
}
Basically what’s happening, is it checks for the player entering the trigger, and if the stairs’ (polygon) collider isn’t enabled, it sets the isInTrigger bool to true, which in turn activates the stairs (after checking for input). It’s pretty much just creating a custom OnTriggerEnter check, which allows the system to loop back and produce a different result.
So, I can enter a trigger, activating the stairs. And I can either walk to the other trigger to deactivate them, or I can walk back to the same trigger, which will also deactivate the stairs.
Trigger problem (SOLVED, solution below)
The problem comes when I deactivate the stairs. Basically what I want, is for the UI Text to appear and for the player to be able to activate the stairs, at any time they are in a trigger.
However, because of the way this system works, if you enter a trigger while the stairs are on (to deactivate them), the “if” statement in OnTriggerEnter2D will never execute, since the stairs are enabled at the time. This results in the “else” statement executing, and the system thinking you are not in the trigger.
Because of this, I have to leave the trigger, and return, for it (the stairs system) to be usable again.
How can I fix this?
__UI Text problem (SOLVED, solution below)__
The UI Text is just a single letter following the player (above their head), to indicate what they have to press.
The problem, is that I need some way for it to detect whether I’m at the top of the stairs, or the bottom, and for the letter visible to be based on that.
So if I’m at the top of the stairs, I need it to say “S” for going down, and if I’m at the bottom, it should say “W” for going up.
Currently for buttons work on both ends, but I can fix that myself once I get the text working.
How do I do that? I was thinking of ray casting, where the cast goes from the center of the stairs to the player, and if it points up from its origin, “S” is displayed at the trigger, and “W” for when it’s pointing down. But I don’t know how I would do that.