The “Transformers” films, as befitting a series spun out of a Hasbro monster-truck toy system designed to connect with the inner worldview of nine-year-olds, started off, in 2007, as exceedingly wholesome. What a difference a decade of baroquely semi-coherent robot-fury overkill makes! “Transformers: The Last Knight,” the fifth film in the hugely popular, critically reviled franchise (has there ever been a movie series that put the red state/blue state divide between audiences and reviewers like this one does?), is also the most extravagantly brutish and lurid. There’s still a PG-13 gee-whiz-ness to the proceedings, but the towering, swivel-socketed machine men now seem like they’ve been around the block a few times, complete with pit stops at the race track and dive bars.

The Decepticons — the fey gangsta Mohawk, the goofy bikerish Nitro Zeus — look as if they might be auditioning for “Suicide Squad 2,” and their leader, Megatron, skulks around with the angriest possible attitude, his face marked by a blood-red splash. The good-guy Autobots come off nearly as wasted: Bumblebee is introduced by getting blasted to pieces, and even the stalwart superhero Optimus Prime has slipped over to the sinister side. He has made a deal with the alien sorceress Quintessa, who looks like a very expensive hanging necklace, to salvage the Autobots’ dessicated home planet, Cybertron, by sucking the life out of earth.

There is, in addition, a medieval backstory that returns us to the days of King Arthur, but even this potentially stodgy premise is staged in a heavy-metal Stonehenge-meets-bloodshed way that puts the dark back in Dark Ages. All of which makes “The Last Knight” the first “Transformers” movie that could actually be characterized as badass. Which isn’t a bad thing. It may, in fact, be better.

So what does a better “Transformers” movie look like? There’s still a hurtling slovenliness to it — a sense that overly quick cuts and throwaway lines are taking the place of what, in another movie, would be calmly staged dramatic scenes. (Oh, those!) I can only speculate as to why Michael Bay, at a point long past which most producer/directors would have handed off the directorial reins of this series to someone else (hasn’t he — how can I put this? — said all that he has to say?), is still in there, directing this latest installment. It’s almost as if the series fulfills him: Instead of knuckling under to the system the way he had to do when he made such relatively austere works of artisanal craft as “Armageddon” and “Bad Boys,” here he can just let his destructo action-junkie freak flag fly.

Yet part of what’s exhausting about the “Transformers” films is that hectic bland wholesomeness — the empty energy that can give you a seizure of antic tedium. “The Last Knight,” by contrast, has the somewhat sexier flavor of impending dystopia, and it’s actually, if this can be believed, even more over-the-top than the previous four films. For the first time, the messy hyperactive form and nihilistic crunched-metal content seem to reinforce each other.

Mark Wahlberg has a knack for playing free-floating desolation that isn’t alienated enough to get in the way of his ripped-belly bravura. And though the previous “Transformers” film, “Age of Extinction” (the actor’s first), was so bad it was irredeemable, it’s now clear that he has the ability to ground these movies — to stand up to the metal — in a way that the softer, flakier Shia LaBeouf did not.

Wahlberg’s character, the greasy-longish-haired Texas inventor Cade Yeager (sorry, but no matter how often you say it, that doesn’t sound like a name), is off the grid, running a sports-car junk yard where he looks after Autobots like Hound (voiced by John Goodman), a stogie-chomping brawler with Neptune’s beard. The rage of the Decepticons lures Cade out of his doldrums, and before long he’s thrown together with Viviane Wembley, an Oxford professor who is surely the first scholar in the university’s history to teach classes in strappy pumps that look like they were purchased during a Kardashian shopping spree.

Viviane is played by Laura Haddock, a British actress whose greatest presence thus far has been as Peter Quill’s mother in the “Guardians of the Galaxy” films, but she’s a real find, with a puckish sensual vivacity, goldfish eyes that stare like laser beams, and an effortless ability to spar. Viviane gets drawn into the fray because she’s the last direct descendent of Merlin, and therefore the only one who can connect with the magical staff that’s buried in his coffin. (I know there are a number of screenwriters to blame, but really, who makes this stuff up?)

Mystical medieval hokum aside, Haddock and Wahlberg generate the kind of hostile sexualized chemistry that is fast going out of style, and a movie like this one can use every ounce of it. The two bicker and pout through a plot that’s like “The Da Vinci Code” crossed with a “Terminator” sequel on Jolly Rancher candies, and they’re accompanied by Sir Anthony Hopkins, who plays Sir Edmund Burton, an elite astronomer who guides the events, but is really on hand as a kind of aging mascot of the happily unhinged. Hopkins helps to spank things along with his can-you-believe-this-is-what-it’s-come-to? reading of lines like “What a bitchin’ car she is!”

The plot of “The Last Knight” turns on the apocalypse (and, therefore, the U.S. military), which lends the usual chaotic jumble of events a bit of organizing heft. In the epic climax of a picture like this one, the visuals tend to mean more than, you know, the meaning, and here the world-destroying energy on hand takes the form of a corrosive weapon that looks like gigantic floating shards of cardboard packing debris. It’s all pleasingly spectacular, and also rather empty — at least, until Optimus Prime returns to his true self, his words spoken by Peter Cullen in a voice of such deep rich square nobility that, coming after nearly two-and-a-half hours of hellbent robot-clanking decadence, he seems a cathartically old-fashioned figure. He reminds you that there are moments when this series is capable of making you think that you like it.