I have a friend who is a priest, and he once said that he’d heard absolutely everything in confession except for one thing; he’d never once heard anyone say “masturbation.” It’s understandable to feel ashamed. Our sexuality touches us in our innermost selves, and discussing anything related to our sexuality makes us feel vulnerable. But if it makes you feel any better, you’re not the first to deal with the problem nor will you be the last. It is a common difficulty for every human being. A sense of shame is good in that it is part of our conscience telling us when something is wrong. However, be careful because if you let feelings of shame dominate you and keep you from dealing with its cause, it will become an opportunity for Satan to lead you into sin and despair.
With that said, be aware of what constitutes mortal sin. See
catholicdoors.com/faq/qu06.htm and
trosch.org/the/mortal-sin.html The primary condition we’ll deal with here is “full consent of the will.” If you are truly doing this while asleep (or nearly so) and not doing it because you consciously decided to do it, it is not a serious sin. In fact it is likely not a sin at all in this case. Masturbation is not a sin because ejaculation or orgasm is a sin (they aren’t sins); it is a sin because it is the misuse of the sexual faculty which is intended for the procreation of children and for the bonding of husband and wife in marriage. Masturbation severs procreation from the sexual act, and instead of bonding you to your spouse, it draws you inward - essentially bonding you to yourself. But “use” or “misuse” implies a “user” - someone who decides to make use of. If it was by conscious choice, it was neither “use” nor “misuse.”
Please do discuss this with a priest in confession. Explain the stuation. Ask him for advice. But also examine yourself. Pay attention to your daily activities and see if there are any correlations between when this occurs and the things that happened during the day or near bed time. It could be something as simple as too much caffeine late in the day, being too exhausted or perhaps from not getting enough exercise/being too idle. It could be that your mind is overstimulated too close to bedtime from watching television, playing video games, or engaging in some mentally strenuous activity. Turn off the TV/computer/video game/phone, etc. and spend some quiet, relaxing time for at least one hour before bed. Read or pray the rosary, for example. Perhaps you spend too much time alone and should spend more time with friends or family as feelings of loneliness or depression can exacerbate the problem.