2.5D Game Development: Puzzles: Part 1 — Elevator System
In the following few articles I’ll be going over some different puzzle elements and how I approached them within a 2.5D environment. This will cover things like elevator systems, wall jumping, and pushing objects onto switches.
I’ve got a simple scene set up here. Once I move up the moving platform there is a box with a red light on it. I want to be able to hit the “E” key, and call the elevator down to the floor I’m on.
The “Elevator_Panel” has this trigger collider on it. So as you probably already know, we need to detect collision with it, and then check if E is pressed. A lot of folks will say using OnTriggerStay() is the way to handle this. I have found a different method that I personally think works much better. This method makes use of bools!
OnTriggerEnter and OnTriggerExit both flip a bool called
isTouchingPlayer on and off. So if isTouchingPlayer is true, and E is pressed the elevator moves down. When the player exits the trigger collider and presses E nothing happens. Same functionality as OnTriggerStay, but I just like to have more control over things, so this method is my favorite.
In the Update function we’ve got a little bit to unpack. Theres a bool called elevatorCalled. Elevator called is automatically false. So if its false and isTouchingPlayer is true, then we continue to the next set of conditions. In the Player script I set up a method to handle the coinCount.
All this does is return _coins. This is an int variable type. I also have a variable of type int in the Elevator script called requiredCoins. So if coinCount() returns a number that is greater than or equal to the requiredCoins the elevator will come down. I set requiredCoins = 8. You can set yours up however you see fit.
Next I’m getting the MeshRenderer of the elevatorButton game object so that I can change the material color when E is pressed. elevatorCalled is set to true and then elevator.CallElevator() is called from the Elevator script.
Here, all I’m doing is setting goingDown(a bool) = to the opposite. That way, if the elevator is above and I press E it comes down and if I’m on it it goes up when I press E. This is an almost identical set up to the moving platforms covered in the last article.
So why is your character falling through the elevator when it goes up? Why is it so jittery? Well be sure to have the elevator movement handled in a FixedUpdate(). Thats not all. We want to make the Player game object a child of the Elevator game object. How?
I set up another trigger collider inside the elevator. When they player collides with this it becomes a child to the Elevator because of the OnTriggerEnter and Exit in the Elevator script.
Now that all that has been set up, the final result should look like this.
Press E, button turns green, elevator descends. Enter the elevator collider, the player becomes a child of the elvator. Press E, the elevator ascends. Exit the collider and now the Player is no longer a child to the elevator. Beautiful! In the next article I’ll go over how to set up wall jumping :)