GameDevHQ 2.5D Cert Requirements Part 7
Objective: Create an Elevator System
Following on from the last post will now make an automated elevator that stops on each floor for a defined amount of time. We will use a lot of the logic from the moving platform such as a list of waypoints that will be the floor stops, a floor number, moving speed, a fixed update instead of update and OnTriggerEnter/Exit with a collider to manage the Player behaviour when on the elevator floor.
For this part we will also use a coroutine to set and execute the time the elevator waits on each floor before moving on. We will need some bool variables to know if the elevator is going up or down and a bool to check if the target floor can be changed(needed because of the time delay on each floor).
We will set the floor number and the bool values in the Start method. The elevator starts at the top so it will be going down to start. We also set the can change to true and set the floor number to start as the max value of the list being the top floor.
Next in the FixedUpdate we move the elevator towards the first target which is placed as itself giving it the starting position. We then run a conditional if to check that the elevator is in position and can change floor is true before triggering setting can change to false and triggering the coroutine.
Inside the coroutine we delay by the floor stop time before checking if the bool for going down is true or false. If going down we check if the elevator is on the bottom floor (0) and if so turn the elevator around and move up floors, if not we move down to the next floor. If the elevator was going up we check to see if the floor is the top floor before changing down back to true. Once the floor has been checked and the next floor has been assigned we set the change floor bool back to true so that the FixedUpdate can once again get the elevator moving.
Finally we have the OnTriggerEnter and Exit to make the Player a child object of the elevator when the Player is on it to fix the jittering issue we discussed in the previous post and earlier platformer series.