Create an Elevator in Unity
Objective: Create a moving platform with elevator behavior to move between levels each 5 seconds.
We have this 3d model that we are going to use as an elevator.
The first step is to create empty objects as a child of the elevator to represent the origin and the final position of the elevator. Then we have to create a new elevator script and attach it to the elevator game object.
In the new script we have to declare 2 variables to store the reference to the point a and b and the elevator platform. Also some variables related with the movement like the speed, the flag to know which direction to move and the delay to stop and start moving again.
In the same script we are going to create a new coroutine to move the platform in an infinite loop. In this coroutine we first check if the platform passes some of the 2 points to move to the other direction. Then we check the current value of the flag and assign an int value to the path variable and finally we call the Translate function to move the platform.
After that we can assign the values in the inspector.
And that’s it!!! We have a functional elevator for our levels. Don’t forget to make it a prefab.