Godot Version
4.2.1
Question
Hello everyone, I’m trying to move an Area2D on the x axis until it reaches a Curve2D (_on_area_entered), then follow this Curve2D with rotation (sample_baked_with_rotation) to the end and then move the position on the x axis again.
I’ve already tried Path2D → PathFollow2D → RemoteTransform2D, unfortunately the transition between position.x += speed * delta and the RemoteTransform2D is very rough.
Then I tried it without RemoteTransform2D and interpolated the position of the Area2D with the position of PathFollow2D, which also worked well, unfortunately only up to a speed of 100, after that my Area2D is in all directions, just not where it should.
Train.position = lerp(Train.global_position, Path.global_position, speed * delta)
Train.rotation = lerp(Train.global_rotation, Path.global_rotation, speed * delta)
To save myself the multiple instances of PathFollow2D I have now switched to sample_baked_with_rotation, which doesn’t work badly, but only if the Path2D/Curve2D is in the same scene as my Area2D. If the Path2D/Curve2D is in another scene, it is always rotated by 90 degrees (see screenshot) and I cannot get it back into the correct position. Even when I rotate the Path2D and it looks right in the editor, my Area2D moves from bottom to top.
Here is the complete script:
extends Area2D
var speed = 100
@export var path2d: Node
var curve2d: Curve2D
var currentDistance = 0
var isFollowingCurve = false
func _ready():
curve2d = path2d.get_curve()
func _process(delta):
if isFollowingCurve:
followCurve(delta)
else:
position.x += speed * delta
func followCurve(delta):
var totalDistance = curve2d.get_baked_length()
currentDistance += speed * delta
if currentDistance >= totalDistance:
isFollowingCurve = false
var result = curve2d.sample_baked_with_rotation(currentDistance)
result.origin = result.get_origin() + path2d.global_position
transform = Transform2D(result.y, result.x, result.origin)
func _on_area_entered(area):
if area.is_in_group("Gleis_Geschwungen"):
isFollowingCurve = true