Godot Version
Godot v4.2.2.stable.official
Question
I’m playing around with Godot making a simple platformer, and I was following Brackey’s tutorial then adding stuff on my own. Something I’ve been struggling with for several days now is moving platforms – I’ve been trying to create a moving platform that doesn’t use an animation player. This is because I want to be able to add a platform that can adapt to the environment: it would move up until it hits something, then down until it hits something, etc.
The issue I’m running into is that when my player character is riding up, it moves downwards into the platform. When riding down, it floats above the platform. This gets more pronounced the faster the platform moves.
I am aware that this has been a common issue since I find many references to it online, but I haven’t been able to find a solution among them. Almost every tutorial on moving platforms utilizes an animation player and manually determines the platform’s path. I have attempted many iterations, but I will attach two of the approaches: one using CharacterBody2d and the other AnimatableBody2d.
My player character uses CharacterBody2d. Here is the code for the platform (for AnimatableBody2d):
extends AnimatableBody2D
@export var move_speed = 1
var move_direction = Vector2.DOWN
func _physics_process(_delta):
move_and_collide(move_direction * move_speed)
func _on_top_sensor_body_entered(body):
print('top sensor collision')
print(str(body))
move_direction = Vector2.DOWN
func _on_bottom_sensor_body_entered(body):
print('bottom sensor collision')
move_direction = Vector2.UP
Both examples have the issue described above where the character moves up/down a bit depending which way the platform moves. Funnily enough, using AnimatableBody2d with move_and_collide and checking “Sync to Physics” seems to works how I would like, but produces errors saying that move_and_collide should not be used alongside “Sync to Physics”.
If you take a look at the examples, the relevant code is in res://moving_platform/moving_platform.gd. To test it, you can run the project then hop onto the moving platform closest to the player character. wasd for movement, space for jump.
AnimatableBody2d Example
CharacterBody2d Example
Thanks in advance for any help.