Creating a simple elevator system in Unity with the new input system.

Luke Duckett
7 min readJul 20, 2021

--

In this article I will go over how to create simple elevator system that requires pick ups before being able to be activated. It will be modular allowing more to be built on.

In my scene for the call button I have a small cube with a sphere on it. The sphere is coloured red and will change to green when successfully changed. Attached is a box collider trigger and rigid body. This is used to detect when the player can interact with the object.

The elevator can just be a simple 3d cube platform. I’ll replace this object and place the duplicate in the final position for each floor the elevator can move to. Then remove all components. We will assign this to the elevator later.

The first step is to create the scripts. We are going to create two scripts. The first script will be related to the elevator and the second will the elevator panel.

For the elevator we want to be able to be called from another floor, and receive input from the player when they are in the interactable area. Lets create a small UI panel with text showing up or down

I used buttons as the visuals as this then allows the script to be updated later to receive click events as well). This panel should be inactive by default.

On the elevator script we will create a list for storing all available floors (this will be our ‘stops’) and an int value which is the max amount of floors we have (floorList.Count -1 as the list is zero indexed but the count is not).

We will need a speed variable and two bools, one for determining if the elevator is interactable and one to determine if it is moving. This is so we can prevent the player from switching floors halfway through a trip and we wont react to the player input if the player isn’t in the elevator.

Next we need a function we can call externally by the elevator call button to move the elevator to the correct floor. This will take a floor number (which will match with our floor list). This will set our target and set the moving variable to true.

After this we need a function to actually move our elevator. This function will be called in fixed update and just moves the elevator to the target position.

The UI Manger has been set up as a singleton and has a function which takes a bool value and uses this value to set the elevator panel active status. We will call this function each time the player can interact with the elevator.

Our next step is to create a function to check if we have arrived at the location. We use “Vector3.distance” for this and when we arrive we set the moving variable to false. We also can set the elevator panel to active if the player is on the elevator when it arrives.

Now we need to react to the player. We can use the on trigger enter and on trigger exit values to set our elevator panel to active when the player is in the box collider and allow the elevator to be interacted with.

Next lets set up our up and down functions. The functions will increment or decrement the next target, cause the elevator to move and hide the elevator panel.

Next we will need so set up the input actions. For setting Input Actions up from the start see my article here.

Open the input actions asset and add in a new input action. Mine is called “Pick Floor”. Your action type should be value.

Right click on your action and select “Add 2D Vector Composite” Delete the left and right actions, and then map the up and down to the “A” and “D” buttons.

Now add an action for a button press and call this interact. I’m setting the binding to “E”.

Back on our elevator script we will create the function which will react when that action is called. In this function we check to see if the elevator is interactable and not already moving. We read the value from the callback context (the vector2.y) and if its positive it means the up button was pressed and if negative the down was pressed. We then can call the appropriate function.

Next we need to set up our call button. If you haven’t already, create a new script for the call button. We need a variable for the player, our mesh renderer, the connected elevator, if the state if currently interactable and our floor number.

In our start function we will get the mesh renderer (from our child game object which was the coloured 3d Sphere) and a reference to the player.

We want to set the colour of the button to show the player its interactable. I’ve created 3 helper methods to change the material colour based on the state. One to reset the colour to red, one to change to blue to show it can be interacted with and the final one to show the interaction was successful. We will only set the colour to blue if the player has enough coins (which we will implement below.

Next we can set up our triggers for when the player enters the collider. Here we will just set the “_isInteractable” to the appropriate state and set the colors of the material.

I will be ensuring the player collects all the coins before the elevator is able to be interacted with but this can easily be changed to requiring a key card etc. On my player script I have a private coin count variable and a property with a public get, which returns the coin count.

Now we can create a function that takes the input action call back. In here if the player pressed the interact button while the “_isInteractable” state is true we call the elevator using the elevator reference (assign this in the inspector) and passing in our floor number. We check the players coins and if they have enough we call the function and also set the material to green so the player gets feedback their action is successful.

The next step is to add our floors to the elevator list (in the correct order) and ensure our elevator is assigned to our call button.

Next we can add our functions to our player input component (this is on our input manger, see previous articles for details on setting this up).

Now we can test our function.

That’s all for now.

--

--

Luke Duckett
Luke Duckett

Written by Luke Duckett

🎮 First Nations Unity Dev from Wonnarua country 🏞️ | From Player to Lifelong Learner: Crafting Games, Debugging Code, and Embracing New Technology

No responses yet