Moving Platforms
Moving Platforms
One of the most iconic objects in a platform game is…a platform. But I’ve been experimenting with them and look, they are kinda complex if you asked me. There’s this variant called Moving Platform which has an interesting property: it moves.
Heh, you might be thinking it is a mundane feature, right? It’s not! It’s complex to handle movement trying to maintain two objects in synced motion.
Well, turns out that Godot has some small secrets here and there that help us achieve proper moving platforms. Let’s start with the Player.
The Player
Did I need a chapter just to tell you that we have to use move_and_slide_with_snap
for this mechanic? Maybe not, but now that I’ve done it…
We have to use a KinematicBody
for the player and move it using move_and_slide_with_snap
, you can read the Slope Movement experiment to know how to use this method properly. Or you can download the Moving Platform experiment and just start from there.
take the chance to make a donation, pretty please
The reason why we need to use the move_and_slide_with_snap
is because…it ensures the Player snaps to the moving platform. So as the moving platform moves, the Player will stay in place. So we don’t have to handle this manually syncing their velocity. Quite convenient, right?
The Moving Platform
Now here comes the real trick…Something that people don’t realize quite often is that
IF A BODY MOVES IT IS KINEMATIC
I say that to my past self all the time, how could I not think about that back then. I used to try with StaticBody2D because, duh, it’s just a platform, KinematicBody2D is for players and whatever. But don’t be naive, Godot knows when something moves or not. Don’t try to move a StaticBody, because it’s meant to be static.
So the root of our moving platform is a KinematicBody2D, let’s add a CollisionShape so it is capable of detecting collisions. And just to have some graphics on the screen I’ll draw a rectangle with Polygon2D.
Alright, so turns out KinematicBody2D has a property named Sync to Physics. Let’s read what the documentation says about it:
If true, the body’s movement will be synchronized to the physics frame. This is useful when animating movement via AnimationPlayer, for example on moving platforms
What a coincidence, right!? It’s almost as if when we read the documentation we learn how to do stuff, it’s magical.
Let’s toggle this property on the Inspector and…I dunno, maybe animate the movement via AnimationPlayer?
Using an AnimationPlayer I’ll animate the platform movement, it’ll be like an elevator it goes up then down.
Now, there’s a small problem. See, we are animating the Platform position, right? So if we instance this Moving Platform as it is, as soon as we play the animation it will go back to the position we animated it.
To be able to properly position the Moving Platform we need to make a Node2D the root of the scene and use it to offset the position properly because with that the animation on the KinematicBody2D will be relative to the parent.
Now we can properly position our platform in the game’s world and as you can see it works wonderfully.
Well, there’s a small inconvenience with this approach which I think you’ve already noticed. It is very inconvenient to create keyframes for each and every movement you want your Moving Platform to perform, plus you’d need one animation for each different moving pattern…Fear nothing, I’ve already solved that and you’ll be impressed with how simple it is, coming next week.
Leave a comment below if you are liking the project so far, also let me know if you have any question. By the way, don’t forget that we can discuss this and more topics on the Community and especially, you can make requests for more experiments on the Requests board!
That’s it, thank you for reading. Keep developing and until the next time!
Files
Get Gamedev Experiments
Gamedev Experiments
A collection of gamedev experiments cut down to small digestible projects
Status | In development |
Category | Assets |
Author | Pigdev |
Genre | Platformer |
Tags | Creative, flat-shading, Game Design, godot-engine, Open Source, secrets, sourcecode |
More posts
- Selection BoxAug 06, 2020
- Selectable ObjectsJul 30, 2020
- Jump with SnapJul 24, 2020
- Jump and Fall Through PlatformsJul 23, 2020
- Path Follow PlatformsJul 15, 2020
- Tilting on SlopesJul 01, 2020
- Slope MovementJun 27, 2020
Comments
Log in with itch.io to leave a comment.
Interesting post. Please continue.
I'm going to share it on Twitter...
Thank you, will do! :D
Yes - please continue.