How do I have the PathFollow2D's progress be based on the horizontal position of the player?

4.6.2.stable

Hello, I am trying to add rail grinding into my game. I don’t want the player always starting at the beginning of the rail when entering it’s Area2D at the middle or end of the rail.

I want the PathFollow2D follow the player x coordinates so when the player starts rail grinding at the middle or the end of the rail, the progress of the PathFollow2D starts at that position.

Any help is appreciated.

There is Curve2D::get_closest_point() you can use to determine the closest point on your Path2D::curve, or Curve2D::get_closest_offset() to get the offset in pixels.

How do you implement this? I tried this but it come with an error:

$PathFollow2D.progress = curve.get_closest_point(player.position)

Here is the error:
" Invalid assignment of property or key ‘progress’ with value of type ‘Vector2’ on a base object of type ‘PathFollow2D’ "

Try get_closest_offset() instead.

It works, thanks for the help!

1 Like