How to make collided object return to initial position? I need a simpler way to my method.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kronopy

I am working on a small platform game. The character will kill the enemies by hitting the bricks from under them, like Super Mario Bros or Bonkheads. When I hit the brick, it must move above for few pixels (around 10 I think), then return to it’s initial position. When an enemy is on the brick the enemy must also jump and fall to another action (probably death) on the initial position (back on the brick). I’ve been trying to figure out how to make it, and the Idea I come up with is to add multiple layers, but the way I think of making it seem overly complicated. My idea is:

layers 1 → collision shape on the brick for walking.
layer 2 → collision shape with static sprint above the brick to force the brick to return to initial position once is hit. I am basically pushing the brick trough the collision shape, by making the brick less heavy than the player.
layer 3 → area2d/body_entered over layer2 to make enemy go into jump animation and death.

To be honest I am not even sure if this would work overall (I anticipate many complications as the enemy get diversified), is just some physics I discovered by accident while playing with the engine doing Pong.
I am sure there must be a much simpler way, and I watched trough almost every of tutorial for Mario clones and such, but there was nothing helpful, everyone seems to hate bricks and destroy them TT TT This is a project I started as a way to learn the engine and familiarize myself with the script, but I am growing fond of it and thinking of making it into a full game. Using my method for 100 levels sounds like… I need something more minimalist.

Also, can you recommend me the best choice between Kinematic2DBody and RigidBody for this?

:bust_in_silhouette: Reply From: johnygames

The way I see it you do not need multiple collision layers. All you seem to need is a boolean variable that turns to true after a brick has been hit. Have the bricks detect collision with the enemy at all times and check whether an enemy is colliding with the brick that was hit.

Two conditions must be true for the enemy to go into death animation (or whatever animation you want): The enemy must collide with the brick AND the brick must have its boolean value set to true, which it will when the player hits it. If both those conditions are true. any enemy standing on said brick should do what it’s got to do.

Does this help?