[deleted]
There is a psychological theory about this it's called the third man factor. When our minds undergo extremely stressful and taxing situations such as religious person fasting to near starvation, soldiers fighting, exploreres and even people who have been through a violent or frightening experience.
Ernest Shackleton experienced this on his final leg of the journey. He says he saw a man showing them the way to to the whaling station on South Georgia and as soon as he saw the station the man was gone. Climbers of Everest have experienced this to wheb a random climber will appear and lead them to safety.
A famous legend around this in war time would be the Angels of Mons. It was the first major engagement between British forces and the advancing German army during WW1. The British out numbered and out flanked were smashed and had to quickly retreat. During the retreat many soldiers saw angels watching over them and protecting them during the retreat.
One of my favourite ones featuring the third man factor (please forgive the lack of details I can't find the exact area in the book) is of an explorer trekking his way back from the jungles of Papua New Guinea. He wanted to give up when he claims to have seen on the hill just over from him. He called out but the man never answered he followed him days until on one hill he wasn't there. When he came upon the hill he had reached the coast and was able to reach civilisation and help.
I don't know if this answers your question in the way you wanted but it should give you an understanding of one reason people have visions.
Source: The Third Man Factor by John Geiger.