Horizontal Platforms

Hello, I am trying to make a 2D platformer game. I am able to create horizontal and Vertical moving platforms. But the issue i am having is that when the player lands on the horizontal platform they don’t move with the platform.
I have read that you need to make the platform a parent of the platform but I do not know what that means or how that works. Does anyone know of a tutorial or code sample that has that that i could look up.

Thank you

(I used the Zenza Tutorial and based my platforms on the moving enemies)

The platform is auto shifting once you’ve landed on it, sliding left to right, right to left etc ? If so no tutorials but if I was doing that myself I’dd probably use collision object to detect the platform the user is on then keep his x index updated with where it should be relative to the start and current world x of the platform

Check out Platypus, a 2D platformer engine which can handle moving platforms amongst other things.

1 Like

(Edit: This worked for my needs but I do hope someone with more experience in Defold and Lua can shed some light on how to properly approach this.)

I had the same problem some time ago. I made the Zenva platformer tutorial and afterwards, I wanted to see if I could translate some of what I’d learned by remaking the Godot Brackey’s tutorial in Defold (including using the enemy movement as a basis for the moving platforms). There were some references to what you’re proposing, parenting the player to the moving platform, on this forum from older posts, but no examples I could borrow from. I didn’t know how to use extensions, so I couldn’t rely on those. Plus, I really wanted to see what I could do with vanilla Defold.

So this is basically how I trial-and-errored my way to a solution: In on_message, just check if the message_id is a contact point response and if the message is from a group you’ve probably named “moving platform”. If it is, then set the moving platform as the parent of the player. Like this:

if message_id == CONTACT_POINT_RESPONSE then
		if message.group == GROUND_LAYER then
			handle_obstacle_contact(self, message.normal, message.distance)

		elseif message.group == hash("moving_platform") then
			go.set_parent(go.get_id(), message.other_id, true)

Now, if your player lands on a moving platform, it should move along with it like so:

It works okay at first glance, but the moment you get off the platform, the player still moves like its parented to the moving platform. So you have to add an else statement at the end to unparent the player from the platform when they are no longer colliding, like so:

else
		go.set_parent(go.get_id(), nil, true)
	end

So the entire snippet should look like this:

if message_id == CONTACT_POINT_RESPONSE then
		if message.group == GROUND_LAYER then
			handle_obstacle_contact(self, message.normal, message.distance)

		elseif message.group == hash("moving_platform") then
			go.set_parent(go.get_id(), message.other_id, true)
else
		go.set_parent(go.get_id(), nil, true)
	end

Then it should work fine.

I don’t know if this is the best way to do this, btw, I just know it worked for my modest use case.

Also, welcome to the community!

4 Likes

This is the information I was looking for, thank you. I tried to implement it last night but was not successful. I need to see what else I did for my platforms and spend a few nights messing around with it.
I appreciate the info and will answer back when I get it sorted out

1 Like