This solution is really resource intensive, there may be a better one!
Actually i just simplified the problem to 2D, here's a working solution for 3D:
Once:
/scoreboard objectives add dir dummy
/summon ArmorStand ~ ~5 ~ {NoGravity:1b,CustomName:"move"}
Repeat:
/scoreboard players set @e[type=ArmorStand,name=move] dir 0
/execute @e[type=ArmorStand,name=move,score_dir=0] ~1 ~ ~ summon ArmorStand ~ ~ ~ {NoGravity:1b,CustomName:"move"
/execute @e[type=ArmorStand,name=move,score_dir=0] ~1 ~ ~ scoreboard players set @e[type=ArmorStand,name=move,r=0,c=-1] dir 1
/execute @e[type=ArmorStand,name=move,score_dir=0] ~ ~1 ~ summon ArmorStand ~ ~ ~ {NoGravity:1b,CustomName:"move"}
/execute @e[type=ArmorStand,name=move,score_dir=0] ~ ~1 ~ scoreboard players set @e[type=ArmorStand,name=move,r=0,c=-1] dir 2
/execute @e[type=ArmorStand,name=move,score_dir=0] ~ ~ ~1 summon ArmorStand ~ ~ ~ {NoGravity:1b,CustomName:"move"}
/execute @e[type=ArmorStand,name=move,score_dir=0] ~ ~ ~1 scoreboard players set @e[type=ArmorStand,name=move,r=0,c=-1] dir 3
/execute @e[type=ArmorStand,name=move,score_dir=0] ~-1 ~ ~ summon ArmorStand ~ ~ ~ {NoGravity:1b,CustomName:"move"}
/execute @e[type=ArmorStand,name=move,score_dir=0] ~-1 ~ ~ scoreboard players set @e[type=ArmorStand,name=move,r=0,c=-1] dir 4
/execute @e[type=ArmorStand,name=move,score_dir=0] ~ ~-1 ~ summon ArmorStand ~ ~ ~ {NoGravity:1b,CustomName:"move"}
/execute @e[type=ArmorStand,name=move,score_dir=0] ~ ~-1 ~ scoreboard players set @e[type=ArmorStand,name=move,r=0,c=-1] dir 5
/execute @e[type=ArmorStand,name=move,score_dir=0] ~ ~ ~-1 summon ArmorStand ~ ~ ~ {NoGravity:1b,CustomName:"move"}
/execute @e[type=ArmorStand,name=move,score_dir=0] ~ ~ ~-1 scoreboard players set @e[type=ArmorStand,name=move,r=0,c=-1] dir 6
/execute @e[type=ArmorStand,name=move,score_dir=0] ~ ~ ~ kill @e[type=ArmorStand,name=move,r=0,score_dir_min=1]
/execute @e[type=ArmorStand,name=move,score_dir=1] ~ ~ ~ kill @e[type=ArmorStand,name=move,r=0,score_dir_min=2]
/execute @e[type=ArmorStand,name=move,score_dir=2] ~ ~ ~ kill @e[type=ArmorStand,name=move,r=0,score_dir_min=3]
/execute @e[type=ArmorStand,name=move,score_dir=3] ~ ~ ~ kill @e[type=ArmorStand,name=move,r=0,score_dir_min=4]
/execute @e[type=ArmorStand,name=move,score_dir=4] ~ ~ ~ kill @e[type=ArmorStand,name=move,r=0,score_dir_min=5]
/execute @e[type=ArmorStand,name=move,score_dir=5] ~ ~ ~ kill @e[type=ArmorStand,name=move,r=0,score_dir_min=6]
Best Answer
Commandblocks only run while they are loaded. Because bedrock doesn't have spawnchunks like Java that are always loaded, once you're too far away (determined by your simulation distance) the chunks the commandblock is in are unloaded and it stops working.
To make sure the chunk that the commandblock is in stays permanently loaded, you can use the tickingarea command.
For example, assuming your commandblock is located at 10 20 30:
and once you're done you should remove it again