Day 93:Create an Elevator In Unity Part 2

Tyler Smallwood
3 min readNov 22, 2021

Objective: Call the elevator and move the elevator to the correct spot and move back on while the player is on the Elevator.

It is time to start working on moving the Elevator. For prototyping we will just use Cubes to create a make-shift Elevator. We will attach the new Elevator Script.

To start off the script we will need to have a reference to track the position of _targetA, _targetB, and the _currentTarget. We will need to be able to control how fast the Elevator moves with a float variable. Finally we will need a bool to know if we can move down or up.

To start off we will assign the _currentTarget to equal the current position. We will do this in start.

In FixedUpdate we will start to check the to see which bool is true or not. If _moveDown is true, the_currentTarget will be set to _targetB and if _moveUp is true the _currentTarget will be set to _targetA.

Now we will set the elevator’s transform.postion to Vector3.MoveTowards(); Move.Towards will take in the current transform.position and set the target destination to the _currentTarget’s position. Next multiply the speed variable by Time.fixedDeltaTime.

Next we will use OnTriggerEnter to assign the player as a child to the elevator. This will help against jittering on the elevator’s movement. Next we will see if the elevator is equal to _targetB and if it is with the player on the elevator, _moveDown to false and _moveUp will be set to true.

We will need to remove the player from being a child of the elevator, so we will use OnTriggerExit to do this.

Now to call the elevator from the panel. We will create a new public method called CallElevator(). We will check to see if the elevator is equal to _targetA and if it is we will set _moveUp and _moveDown to true.

Now back on the panel elevator we will call this method.

Now we have a working elevator!

Thank you for reading and have a wonderful day!

--

--

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer