I have the same problem, its actually a disorder called Paruresis. I have it pretty extreme, its terrible. I don't know why it happens to me, it started back when I was in middle school though. It sucks, I can pee in my house and some of my friends houses. But when I am in school, I have to wait until the bathroom is completely empty to pee, and if I am in a public bathroom somewhere else I can go if there is tons of people. Its weird.
But don't worry, your not alone, I have the same problem and I fucking hate it.