What you are describing is called psychogenic ED. It means that your emotions are inhibiting your ability to get an erection.
Good news is that it can be treated with medications like sildenafil. In a review looking across 27 studies, 2/3 of men with psychogenic ED were able to have successful intercourse after using sildenafil.
However, should it persist, you can consider speaking to a sex therapist to help unlock your sexual desire.