All good melodies have a strong rhythmic foundation which generally remains fairly consistent throughout the phrase. You can identify the tune of Star Wars, Indiana Jones, The 5th Symphony, Super Mario Bros., Zelda, Smoke On The Water, Pirates of The Caribbean, Morrison's Jig, Highway To Hell, etc. just by tapping out the rhythm on your desk.
The next aspect is contour. Usually, the melody has only one highest and one lowest point which are not repeated in the phrase and if you drew a line through the notes, you will see that it forms a clear shape/path. In most tonal works, the melody usually clearly implies a harmonic progression because the melody will change modal frames at regular intervals.
Then there is structure. Pretty much every good melody fits within 8 or some multiple of 8 bars and often in a sentence/period structure or some variant of. 6 and 12 bar phrases also work. Usually, the final bar in the phrase comes back to the tonic, but also offers a pickup that would loop back to the start.
So the easiest way to learn how to write melodies is to start with the rhythm and work outwards from that until you can just do all the components at once without thinking about it.