Nerd For Tech
Published in

Nerd For Tech

Create a Elevator in Unity

2.5D Game Development

Objective: Create a call system that makes gameobject move up and down.

This is our scene and whenever player has collected the required amount and stands near the elevator caller i.e the gameobject that looks like a door, when we press the ‘E’ key, the red indicator will turn green and elevator comes down.

Lets create some variables first inside the ElevatorPanel script that goes inside the elevator caller. The indicator variable references to the red indicator, bool variable is to check if the elevator is called and required coins int variable is to check if player has collected enough coins before calling the elevator and moving on to next stage.

Now after player enters the trigger, check if it is player, then check if ‘E’ key is pressed and the player has collected the required amount of coins. Now that both returns true, check if elevator is already called or not. If not, then change the indicator to green for affirmation and set elevator called to true so that player cannot call it again and again.

Then find the Elevator gameobject and call the CallElevator function.

After player exits the range of elevator caller, set everything to default.

Now create another script Elevator and add it to the Elevator gameobject. There, create a bool to check if elevator was called, vector3 variable to store the destination, gameobject variable to store player, speed to define movement speed and Transform variables to store the position value of origin and target.

Now everytime, the elevator is called, check its position. If the elevator is in origin then move to target i.e TargetB and if it is in Target then move to origin i.e TargetA and set the elevator called bool to true.

Then after player enters the elevator, move the player gameobject to make elevator its parent so that player moves along with elevator. Then check if the elevator is nearly if not equals to the destination to make sure that the destination doesn’t change mid movement.

Then if the player presses ‘E’ key, call the call elevator function which changes the destination and sets elevator in motion.

After player exits the elevator, set its parent to null so that player does not move with respect to the elevator.

Finally move the elevator when elevator is called towards its destination. After elevator reaches the destination, set elevator called to false.

And this is the result.

Check here for complete scripts.

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store