I've only ever written one fanfic, and it's only loosely based on the source material, but when writing, I always remember two things:
1. Plan everything out beforehand.
This is crucial, ESPECIALLY when you're dealing with material that's already present. If you're lenient about breaking lore, then forget about this part, but if you're not, then make sure everything's plotted out from beginning to end. Doesn't matter that you don't plan on ending it, just write an outline ahead of the chapters.
This is to prevent any Deus Ex McGuffin's you'd try to pull during the initial run of your story. The best way to prevent breaking lore is to make sure everything's glass-case enclosed beforehand. Research, and research well. Don't create scenarios than find solutions. If this is an OC fic, then you're more than welcome to. But if you're writing a fanfic, especially one regarding a game, think about a problem solver, then create a problem around said solver.
Nothing makes a fanfic reader's cock harder than seeing a game's mechanic being presented as a plot point in the chapters, and I'm saying this as a fanfic reader (occasional) myself.
2. Understand the pre-established characters before going in.
This rings especially true for shippings. I don't mind some married milf doing a little dirty Sanchez with another character she has no business being with. What I mind is a flimsy reason for doing so, or worse, no reason at all. It's not that hard to make a character do something. Influence them towards a direction where their actions coincide with their goals.
More importantly, make sure they stay IN character. Like stated a few posts above, Sasuke can act as a playboy, but give him a reason to, along with a playboy role that's done in his signature aloof personality. Maybe he wants to fuck Naruto because somehow or rather, his bussy is the goal to reviving the Uchiha clan or some shit. I don't read Naruto.
Don't just make a character do something for no reason. Best part about a fanfic is seeing a character do stuff besides the canon material. The best balance is a fanfic that fulfills the reader's desire while keeping true to the canon. It makes no sense that Bakugo wants to fuck Deku out of nowhere, but if you establish Deku as a McGuffin that will bring Bakugo to his goal, that'll elevate the fucking from a porno parody to a What If...?-level product.
That's really all. There's not much difference between writing a fanfic and an original book, only that the former is more linear but provides established material to work with while the latter is, well, vice versa.