How to make player(platformer game) can push(move) a block and he can stand on top of the block aswell

[*Platformer game]

I want to make a player can push a block but he can also stand on top of the block.

I have tried using physics 2D like this
For example:

i have 3 Sprites

“Player” ( behavior = Platform character , physics 2D [dynamic] )

“ground” ( behavior = Platform, physics 2D [static] )

“A block” [ behavior = Platform, physics 2D [dynamic]

And then when i play it and try to push a block, it not work properly.

1 Like

I have done a box platform pushing event before, using Platformer and PlatformerObject behaviour, but not using Physics2D, so not sure if you’re interested?
I lack experience in Physics2D, so I would like to see screenshot of your events and maybe Player/Box Physics stats so far, to achieve this - it will make the situation more easier.
You can also explain what you want - what exactly is “not working properly”?
Although, my guess is that the player’s mass or the damping/friction parameter is less than the box? You can try modifying them, make the player more “heavier” than the box.

i meant when the player push the object its not move smoothly and yeah it seems that box is too heavy.

btw, you have other solution without using physics2D can you please show me how you do that? thank you :slight_smile:

It sounds like you only need to adjust some of the Physics settings. Check out the wiki, all the settings are explained there.

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.

To conclude…
It’s long.
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…)

1 Like

Could you show the events that way with platformer and platformer object?

1 Like

Its brilliant idea, thanks bro! it work

1 Like