I have not done this myself in Godot, but pulling from previous game development experience, something like the code below should do the trick:
extends StaticBody2D
export (NodePath) var path_to_end_point;
# I'm going to assume the starting position is the starting point
var point_start;
var point_end;
var distance_along_path = 0;
var path_speed = 2;
var going_towards_end = true;
func _ready():
point_start = global_position;
point_end = get_node(path_to_end_point).global_position;
func _physics_process(delta):
# Add distance to the path. NOTE: This will not work with large values in path_speed!
distance_along_path += delta * path_speed;
# Check if the platform is farther along the path, and therefore is beyond the point we want
# to change directions at. If it is, then reset distance_along_path and flip going_towards_end
# so we start going in the opposite direction.
if (distance_along_path >= 1):
distance_along_path = 0;
going_towards_end = !going_towards_end;
# Move the platform from point to point based on going_towards_end.
if (going_towards_end == true):
global_position.x = lerp(point_start.x, point_end.x, distance_along_path);
global_position.y = lerp(point_start.y, point_end.y, distance_along_path);
else:
global_position.x = lerp(point_end.x, point_start.x, distance_along_path);
global_position.y = lerp(point_end.y, point_start.y, distance_along_path);
I have not tested the code, and the code can probably be rewritten/refactored so it is both more performant and clean, but hopefully this gives you an idea on how to go about it.
Also, have you looked the Godot demo platformer project? It has moving platforms as well, and the code looks about what I would expect, albeit I would have written the code differently if I was writing the demo, but I digress.
Hopefully this helps :smile:
Edit: Hopefully fixed the link to the platformer project.