Wang Ping was an important general in the Shu state during the Post-Three Kingdoms period. In the First Northern Expedition of the Shu Han Dynasty, because of the defeat at the Battle of Jieting, Zhuge Liang's Northern Expedition was also a failure. However, Zhuge Liang gained a high degree of trust, but in the Battle of the Street Pavilion, he laid out haphazardly, and was taken down by the Wei army, and was therefore nailed to the pillar of shame of history.
However, in this war, one general became famous in one fell swoop. This general was Ma Mo's deputy, Wang Ping.
In the Battle of Jieting, Wang Ping first persuaded Ma Mo not to gather his troops on the mountain without water sources, and then calmly broke through the siege with only a thousand men and horses. This outstanding performance of Wang Ping not only made him famous, but also was deeply trusted by Zhuge Liang and became an important general in the late Shu Han Dynasty.
However, Wang Ping was actually only a general of Cao Wei. It was during the Battle of Hanzhong that he defected to Shu Han.
At that time, Cao Cao threatened Tianzi to make the princes, Gui, the han dynasty chancellor and the boss of the Cao Wei clique. Although Liu Bei gained the Shu Han Dynasty, his foothold was unstable and his foundation was very shallow. So, why did Wang Ping defect to Shu Han at this time?

(Wang Ping)
For this question, I think there are mainly the following reasons.
First, Wang Ping was forced to defect.
In fact, Wang Ping did not intend to defect to Liu Bei at first, but he was a very small official in Cao Cao's camp, only an acting lieutenant. In ancient times, lieutenants were middle and lower-ranking officers, not to mention agents. Thus, in practice, he could neither dominate the war situation nor his own destiny. When Huang Zhong beheaded Xiahou Yuan, he followed the surrendered Cao army to Liu Bei's camp.
In other words, Wang Ping defected to Liu Bei, which was a bit of a general idea.
Second, Liu Bei was very good to Wang Ping.
After Wang Ping surrendered to Liu Bei, Liu Bei immediately made him a general of the tooth and a general of the yi. This was a mid-to-high-ranking officer in ancient times, and already had the title of "general". It can be seen that Liu Bei was quite kind to Wang Ping.
Why did Liu Bei treat Wang Ping so kindly?
(Liu Bei)
On the one hand, it is because Liu Bei wants to recruit talents. At that time, shu han had just been established, and a large number of outstanding talents were needed. When Wang Ping surrendered to Liu Bei, Liu Bei immediately promoted him to general, which shows Liu Bei's eagerness to recruit talents.
On the other hand, it is also a manifestation that Wang Ping has a brain and does things calmly and meticulously. Later, Wang Ping showed his extraordinary ability in the Battle of the Street Pavilion. It can be seen that Liu Bei is indeed a unique and discerning person.
Third, the Shu Han was a thriving regime.
Although shu han was still relatively weak at that time, the development momentum of shu han was very impressive. In just a few short years, Liu Bei not only united with Eastern Wu, defeated Cao Cao in the Battle of Chibi, and owned Jingzhou. Then, it quickly occupied Yizhou. At that time, he defeated and killed Xiahou Yuan, and it was obvious that he was about to win the Battle of Hanzhong. This thriving scene made Wang Ping look forward to it.
(Cao Cao)
In addition, Liu Bei had a "moral" reputation at that time. He was not only a descendant of the royal family, but also a courtesy corporal, and was obviously a wise lord.
Therefore, although Wang Ping's surrender at that time was forced, there was also an element of initiative and impatience.
(Reference: Romance of the Three Kingdoms)