How can i create railroad tracks with uphill/downhill movement and speed changes in Godot 4.6?

Godot Version

4.6

Question

Godot Version: 4.6

Question:

Hello everyone!

I’m currently learning Godot 4.6 and working on my first 2D game.

I’m trying to create a railroad track system where a character riding a cart moves automatically along tracks that go uphill, downhill, and through smooth curves.

I want the cart to:

  1. Move forward automatically without requiring directional input.

  2. Follow the railroad tracks, including uphill and downhill slopes.

  3. Gradually increase speed when traveling downhill.

  4. Gradually decrease speed when traveling uphill.

  5. Allow the player to jump over obstacles while the cart continues moving forward.

I’m currently experimenting with CharacterBody2D, gravity, velocity, and move_and_slide().

I’m wondering what approach would work best for this type of movement.

Would CharacterBody2D with physics and slopes be suitable, or would Path2D and PathFollow2D be a better option?

I’m still learning GDScript, so any advice, tutorials, or examples would be greatly appreciated.

Thank you!

I’d go with character body, calculating velocity vector depending on the current slope. Path approach might work as well if you don’t plan to have other physics based collisions.