Brihadaranyaka Upanishad 4.4.3 says this.
Just as a moth, jumping from one blade of grass to another, first reaches the tip of the first blade, grasps the next blade, and then releases itself from the first blade. Similarly, the soul, having lost consciousness of the body in which it has lived so far, grasps the suitable body for the next birth, according to the karma vaasanas, and then sheds this body.
Sri Gurupyo namaha 🙏

