Godot Version
4.5.1
Question
`I have two charactersbody, one for the platform that moves up and down and another for the player. The thing is i can’t make move the platform up when the player is on it. Here is my code
Platform:
extends CharacterBody2D
@export var speed: float = 80.0
@export var desp: float = 200.0
var direction := 1
var init_y := 0.0
func _ready() → void:
init_y = position.y
add_to_group(“moving_platform”)
func _physics_process(delta: float) → void:
velocity.y = speed * direction
move_and_slide()
# Cambiar dirección al llegar a los límites
if position.y <= init_y - desp:
direction = 1 # bajar
elif position.y >= init_y + desp:
direction = -1 # subir
func get_direction():
return direction
Player:
func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# --- Tomamos velocidad de la plataforma si el jugador está sobre ella ---
# Handle jump.
if Input.is_action_just_pressed("up") and is_on_floor():
velocity.y = JUMP_VELOCITY
if Input.is_action_just_pressed("shoot") and can_shoot:
shoot()
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
if (direction > 0 and is_facing_left) or (direction < 0 and not is_facing_left):
scale.x *= -1
is_facing_left = not is_facing_left
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
`
You want to use an AnimatableBody2D for a moving platform - not CharacterBody2D. CharacterBody2D is intended for player and enemy/npc movement - not platforms.
Thanks, now i’m trying to move the platform with code but it doesn’t goes down.
Here is the platforms’code
extends AnimatableBody2D
@export var speed: float = 100.0
@export var desp: float = 200.0
var direction: Vector2 = Vector2.UP
var init_y := 0.0
func _ready():
# Ensure movement is synchronized with the physics engine
init_y = position.y
sync_to_physics = true
func _physics_process(delta):
# Calculate the movement amount for this physics frame
var movement: Vector2 = direction * speed * delta
# Update the body's position
# You can use 'position' for local movement relative to its parent,
# or 'global_position' for movement in the global scene coordinates.
position += movement
# Cambiar dirección al llegar a los límites
if position.y <= init_y - desp or position.y >= init_y + desp:
direction *= -1
If you place ``` above and below the code it’s much more readable.
TBH, I literally just coded this yesterday for myself for another project, so I’m going to show you what I did. Feel free to use my code.
class_name MovingPlatform extends AnimatableBody2D
const BLOCK_SIZE = 128.0
## Speed in blocks the platform travels per second. Default is one block (128 pixels).
@export var speed: float = 1.0:
set(value):
speed = value
pixel_speed = value * BLOCK_SIZE
## The direction the platform travels, measured in 128 pixel blocks.
## Negative is left on x-axis and up on y-axis.
## Positive is right on x-axis, and down on y-axis.
## Anything other than 1.0 or -1.0 will alter the platform distance traveled before returning.
@export var move_direction: Vector2:
set(value):
move_direction = value
pixel_move_direction = value * BLOCK_SIZE
@onready var pixel_speed: float = speed * BLOCK_SIZE
@onready var pixel_move_direction: Vector2 = move_direction * BLOCK_SIZE
@onready var start_position = global_position
@onready var target_position = start_position + pixel_move_direction
func _physics_process(delta: float) -> void:
global_position = global_position.move_toward(target_position, pixel_speed * delta)
if global_position == start_position:
target_position = start_position + pixel_move_direction
elif global_position == start_position + pixel_move_direction:
target_position = start_position
Change the BLOCK_SIZE constant to whatever size your Tilemap tiles are. Then set the move_direction Vector2 to the amount you want the platform to travel.
So based on what I’m reading in your code, I’d set the BLOCK_SIZE to 100 and then the move_direction to Vector2(0.0, -2.0) Which would cause the platform to move up 200 pixels in 2 seconds, then back down to its starting position. If it’s moving too fast, change the speed to 0.5 and see how that feels.