While I was in the bath, a good idea came to me.

Literal

Bath [target-に] entering [while-ている-時に], good idea [subject-が] floated.

よい考えが浮かんだ — 浮かぶ (to float) is used metaphorically for ideas 'floating up' into consciousness. ~ている時に marks the temporal context: 'while being in the bath.'