But Actually ...
Optimus and Megatron were never supposed to be enemies -- in fact, they didn't even belong to the same toy line. Basically, Hasbro grabbed two different sets of toys from Japan and paid the Marvel Comics staff to come up with new names for all the robots. The result was Transformers. The same characters already had origins and personalities in Japan: For example, "Megatron" was meant to be a good guy.
Megatron, Soundwave and others came from a toy line called Microman, where the premise was that these little space robots came to Earth and disguised themselves as household items to protect kids. This explains why Megatron (a supposedly giant robot) turned into a regular-sized pistol, but it does not explain why they expected children to have said pistols lying around in their drawers, like in this early commercial:
Meanwhile, Optimus came from a different toy line called Diaclone, which was actually supposed to be battle mechas -- they even included a little metallic figure called "Inch-Man," which represented the pilot. Yes, the horrifying implication here is that Optimus (or "Battle Convoy," as he's still known in Japan) had as much of a personality as, well, a truck. In fact, if you look at him carefully, it's easy to tell that he was always meant to be just another mindless giant robot, like Voltron.
Just another experiment to figure out who would best obliterate Tokyo.