Journal 84 — Unity 2.5D Platformer, Add Elevator Lift

Objective: Add elevator functionality to a prefab

In this article, I will go over how I added an elevator system to a 3D Prefab.

C# Script

In a new C# script, we can attach this to the elevator platform game object. We can include variables for an origin and target empty game object, speed, a wait timer and maximum timer, and a bool to switch the movement up and down.

We can first set the timer values in the Start method. Then in the FixedUpdate method, we can check the boundary conditions where the elevator platform is in comparison to the origin and target locations. We can set the bool true or false depending on the up-down movement

Also in the FixedUpdate method, we can use the bool value to actually move the platform up or down to the origin or target positions.

The last part of the script is OnTriggerStay to set the elevator platform as the parent to the player, and OnTriggerExit to remove the parent when the player leaves.

Implementation

As an initial test, I applied this to the 3D game objects, but ran into some peculiar behavior with the objects. They did not move in a simple up-down pattern.

To check this, I made my own primitive game objects in the same project and made a simple level to test.

I went back and reviewed the game objects in the original level and reached out to some other professionals to get their thoughts. It seems this movement behavior is the result of different coordinate systems between parent and child objects. One suggestion was to take all the individual child game objects, which have a consistent set of coordinates, and make a new parent object near their origin, and see if that corrects the movement.

Sure enough, this worked great, and there were no further issues.

To test this further, I made the lift, platform, and the origin and target objects into a prefab and created a new elevator platform object, and the movement is also correct.

Thank you for your time!

--

--

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
Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.