Godot Version
4.2
Question
I am working on a puzzle platforming game with 4 different playable characters. 1 of the characters has the ability to inverse gravity for himself. This character has a platform of sorts on its head. My idea was that the other characters could stand on top and be lifted up with the gravity character when he enables the ability.
I assumed that this feature would not be too difficult to implement but I’m stuck implementing this feature.
Things in tried:
- add a AnimatibleBody as a child that would move with the main character body. This does not seem to work as the collision area appears to stay in the same place where it is initialised when the character moves.
- add an area2d that detects other characters and copies the velocity to the other characters but it appears that the velocity remains 0 because the player on top is blocking the character from moving.
I think I should be able to make it work by implementing a ongravitycharacter state and have custom physics_process logic but that seems overkill for a seemingly simple problem.
Anyone have any ideas.
You could try your second solution and when the other characters enter the area2d, have their collision layer change to another layer so that the gravety character doesn’t collide with them anymore. You would then have to figure out an appropriate time to switch the collision layers back so they all collide again.
1 Like
I would also go with the second solution, make a GameState which when active will allow the collision to match and collide with the other characters and when you reach the point that you don’t have to collide with them just switch the State and turn the collisions to a different layer so they don’t collide anymore.
When in that Gamestate just add the player’s velocity to the characters you’re moving.
I finally managed t oget it working. I ended up using move_and_collide to move the top player. Originally I thought I could then also use move and collide for the bottom player. But it appears that the collision detection position is not updated until the physics frame has ended. Simply moving the global position of the bottom character seems to work.
var push_distance = Vector2.UP*LIFT_SPEED*delta
var collision = _player_in_direction(push_distance)
if collision:
var pushed = collision.get_remainder()
var top_player = collision.get_collider()
var top_player_collision = top_player.move_and_collide(pushed, false, 0.00001)
if top_player_collision:
if top_player_collision.get_collider() is TileMap:
top_player.state_chart.send_event("death")
global_position += push_distance
else:
velocity.y = -LIFT_SPEED
move_and_slide()
This is by far the most hacky code I have in the project but it works so I am leaving my solution here for others with the same problem.
1 Like