It’s one of first things I coded in GDevelop, so it’s messy and slow. Also complicated. I try to clear up what it does, but just ask if you don’t understand one part.
Gruk’s suggestion is 100% easier.
To start, we’ll use 4 objects: the player (PlatformerObject), the boxes you want to move (Platform), and an invisible hitbox, used to push the object; finally, the “ground” object
Attach an invisible hitbox to the player character (set it to be inside player each frame using Player.X() and Player.Y()). This hitbox is a bit wider than the character itself (ex: width of player is 80px, make the hitbox 88px) - see picture.
#2 > Left-right movement (X Axis)
Picture of the “code”… It’s only there to give you an idea what I mean in below paragraph, so do not copy the code directly.
If this hitbox colliding with pushable box object,
(not pictured) check if box is colliding with player, and
box colliding into ground (set “Ignore objects colliding on edges” to yes)
then check if box is to the right of player, by checking if player X position to the left of box, and player is moving right - you can check this using Animation name or if you flipped your character sprite. If true, then set the box to the player’s current speed. (There is no “get current speed” function for platformerObject, so I had to emulate acceleration using speedX)
then, stop pushing the box object by moving it 2 (or more) pixels away when either player releases Right key or presses Left key.
Vice versa same for the other direction.
Note that I did not code an ability to pull the box (useful when box trapped at wall), but it should be similiar.
#2.5 > Emulating the player’s current speed
It’s not perfect, but it’s good enough to let the player push the box slowly.
This one looks okay so I’ll just post the screenshot and hope you understand.
Essentially it’s just “if player moving increment or decrement (negative) speedX, if player not moving decrease speedX to 0”.
#3 > Gravity simulation (falling)
The box will not fall without physics behaviour, so…
(“bool_inserted” is my code to disable box when it is pushed into goal spot, and no longer needed to be pushed)
Check if box is NOT colliding with ground/platform object, and if the box falling speed is less than a certain amount, in this case 500 pixels/second. If true, increment the falling speed (you need to initialize this variable first in beginning of scene
) with TimeDelta(). (my gravity variable is 800)
If box is colliding with platform (floor), set the falling speed to 0, and use the “Move away from” event once so it’s not sinking in the ground.
(I fear the game can glitch if the box falls too fast)
And that’s it.
There are a lot of ways to optimize this code, but I don’t want to do that - it’s made for a school project in 2018 and I don’t feel like finishing it. If you (the reader) feel like pointing out the mistakes like using “Move away from” events too much, go ahead! (But I won’t do anything about it. Also I’m pretty sure I took a week to code this.)
(Now that I think about it, people would benefit if this was made into a custom behaviour… But I’m still unfamiliar with them…)