Objective: Allow the player to call an elevator
Moving the elevator(or lift if you’re like me) will essentially move in the same way as my moving platforms from the previous article.
I will be using the Vector3.MoveTowards method, that will move an object between two points.
The first thing I did was create a new script and attached it to my elevator. I then changed the tag on the elevator to be Elevator. Remember the tag name that you’re using here for your own object.
For the set up of moving the elevator I needed a few things, two points in game space to move between, speed and a bool for determining if the elevator is going up or down.
I then created a public method that will allow other scripts to update the _goDown bool.
This public method will act as a toggle for the elevator. If it is true when called then it will turn false and the same the other way.
For moving the platform, as this is physics movement this should ideally go in FixedUpdate.
If the _goDown bool is true then I am moving the platform down and take note that I am using fixedDeltaTime. This is because I am inside the FixedUIpdate method.
If the _goDown is false then the elevator is to move up.
To stop the player from getting some jittery movement other way up, I found it better to change the Players parent object to the platform, the same way that I did in the other platforms.
For this to work there needs to be a boxcollider on the elevator that is a trigger. This will make the player a child of the current platform and then when the player exits the trigger the player will no longer be a child of the platform.
Now, moving back to my previously made Elevator panel script. I needed to add a few extra variables for it to work as I want.
I am creating a reference to the Elevator script that I created above, a bool for deciding if the elevator has been called and a string that will allow me to attach this to different elevator panels and move different lifts.
In the start method I now need to grab the Elevator component of the elevator object I want to move. With the way that I am doing this I can adjust the string in the editor to whatever I have in the tag name for the Elevator object. This means that if I prefab the elevator object I can bring it into the scene and only change the 2 positions that it should move between and the tag name. Then, in the Elevator panel that will activate the elevator I change the string in the editor.
The main part of this script hasn’t changed from the previous article, hopefully you’ve read it. The changes I have made are here:
Using the newly created bool for the elevator, I am checking if the bool is true, then the button colour should be red, this is the initial colour and the bool should be false when the button is first pressed. Once the button is pressed and the bool is false, I change the colour to green and then change the bool to true. I then finished the trigger by calling the bool toggle that is in my Elevator script.
And that is it for the code. Now just to tidy up inside Unity. You will need to create your two points that you want the elevator to move between and then drag them in their spots for the Elevator script.