Melody writing has a lot to do with melodic tension and proper resolution. Usually, the degree of tension is determined by the underlaying chord.
An f in the melody over a C major chord has a strong tension and wants to resolve stepwise upwards to the g or downwards to the e, of which both are chord tones. Jumping away from this f to another tension note will feel melodically weaker and not much plausible to the ear. Also if that happens over a chord change and jumping from one tension into a new tension note over a new chord will usually feel pretty awkward. Jumping away from a tension note to a chord tone that is not a step away is melodically possible but can also feel melodically weak. When you’re writing melodies, you should always have an eye at the degree of tension certain notes have with the underlaying chord.
Of course this is again depending on style. For instance Jazz tunes generally prefer to stay in quite strong tensions while other music tries to avoid tensions as much as possible, so your musical language is defining of how much might still be possible and plausible and when it feels musically and melodically disconnected. There are also a few other limitations to that guideline but it’s a good rule of thumb.