This happens when, in viewing mode, the center of the screen, where the player himself should be, coincides with the place where the train is moving.
A reasonable solution that can be made is to move the player from the railroad tracks to a safe place and turn off the protection function before entering viewing mode. And when returning to control the player, turn on the protection function.
The second option is to not react to the center point of the screen (where the player should be) in viewing mode.