using Godot; using System; public class Train : Spatial { [Export] private float _speed = 1.0f; [Export] private NodePath _railwayPath; private Railway _railway; private readonly PathFollow _pathFollow = new PathFollow { RotationMode = PathFollow.RotationModeEnum.Oriented }; private float _distance = 0; // Called when the node enters the scene tree for the first time. public override void _Ready() { _railway = GetNode(_railwayPath) as Railway; _railway.AddPathFollower(_pathFollow); } // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(float delta) { var length = _railway.Curve.GetBakedLength(); // keep track of total distance _distance += delta * _speed; // wrap at 2x total distance _distance %= length * 2; var distance = PingPong(_distance, length); _pathFollow.Offset = distance; Translation = _pathFollow.Translation; Rotation = _pathFollow.Rotation; } // poor mans branching ping pong private float PingPong(float t, float length) { var tml = t % length; return t > length ? length - tml : tml; } }