Moving platform, change position when entering area2d

Godot Version

4.2

Question

I wonder if anyone has some simple way to reposition moving platforms. Like imagine the ones going down waterfalls in your typical donkey kong game. I would like to loop the same platforms over and over when they go off screen and into a collider on a area2d node.

I can do it using a simple script:
extends Area2D
func _on_body_entered(body):
body.position = Vector2(-200, 00)

The problem with this script is that I can only use it to send platforms to specific coordinates. I would like to be able to send them to coordinates relative to the collider. e.g 200 pixels left of the collider. It would make it much more convenient when using the same scene multiple times.

Could someone give an advice?

Also, I use a very simple script to move the platform.
extends AnimatableBody2D

@export var speed = 1

func _process(delta):
move_local_x(speed)

I wonder how to use delta to get an even speed regardless of frame rate. When i try to write (speed * delta) in the script above, the platform will start moving extremely slowly.

I used the AnimationPlayer node to move enemies and platforms in my test platform game. I’m my helicopter game demo I used code as I needed to fine tune the speed of the blades.

I’m not sure AnimationPlayer is going to do exactly what you’re looking for though.

One of the reasons I’m asking is to improve my coding. To be honest the platforms themselves are not very important to me, it would have taken me a lot less time to manually adjust the coordinates for every collissionbox than it has taken me to look for a solution.:smiley: I like the idea of using the animation player but mostly just look for a way to code it, to improve my coding!

1 Like

If the script is on the collider, you can use something like position + Vector2(-200, 0) to get a position 200 pixels to the left of the collider.

If the collider is the child of some other node, you might want to use its global_position rather than position, since position is relative to the parent node.

2 Likes

Worked perfectly. Thank you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.