Underground lift guide - (GMS2 physic engine)

Words
632
Reading
3 min
Listen
Play
4y

fqwfqfqeeeeee.png
Working around some interactive object of differt sort I found really interesting the many ways a lift can be done. This is a guide on how I did it

THE LIFT

qdqdwddqwdqw.png

This is a break down of the object that make the lift work we have the main lift piece the obj_lift. The levers are attached to it and is the moving bridge that the player and other object will collide with.
Then we have the tree colered object that will not be showed during gameplay as they are only sensor to limit the lift movement. Obj_lifttop sto the lift from going upward and obj_liftbottom do the same but instead stop the lift from going downward. Obj_raillift create an area in wich the movement of the lift is enabled if certain condition are met.

INSIDE THE PIECES

gizmo.png

Let's start with the main obj_lift

fewfwef.png

First off the obj_lift checks if it is colliding with the obj_raillift if it is not the vertical speed is set to 0(phy_speed_y is the same of speed_y the change is recall the rules of Game maker physics engine). Then checks how the global variable global.lift is set, depending if is 0,1 or 2
it stay still, move upward or downward. The last 2 place_meeting checks that you see at the bottom are probably an overkill as it already stop it self but gives the idea of an old machinery without too much mainteniance and further stop the lift to going off rail.
The other collision event below the Step_event is used in physics engine to set the collisions otherwise they would not work.

Moving on having cited the global.lift variable let's dive into the levers.

Levers

qdqdw.png

Here's the create event of the left lever here is stored the global.lift variable that when the object is created is set to 0(I'll probably move this variable to the main lift as it make more sense that the main object create the more crucial variable).

Image_speed is set to 0 so that we can switch beetwen active and unactive without having the animation to play continuosly.

delaio is a variable that work like a clock and give some delay to the action of turning on and off the lever.

frqwfwegft.png

Now onto the step event we have the delaio variable that start counting down by subtracting -1 each step of the game.

Then we check if the instance obj_mousecreate exist ( this is done so that when the player respawn the game does not go in error) and we check if delaio is minor to 0.
If this two condition are met we can check that if the mouse is on the lever and if the player is pressing the right mouse, then it checks if the global variable is set to 0 and if we are meeting the invisible area of obj_raillift(sorry I cutted it from the screen).
If it is so global.lift is set to 2 and the lift start moving down.
The image_index of the lever is switched to 1 and the sprite change.
And the delay is set to 30 just a very tiny bit in game but it prevent from comands overlapping.

y =
x =

this are the position to where the lever is glued to the main obj_lift

The last 2 bits check if the lift has reached it course and has to stop or move to the other direction.

adfffwwffw.png

This is the code for the other lever and it works in the same way as above but in reverse.

Area objects

adqwedqw.png

These objects have no code inside them they are just used to delimit areas and the collision with them are in the other objects events.

Thank you for reading and here in the video below there's a demostration of the lift.

Have a goodday!馃槃