William Hjortsberg, who has died aged 76, was a brilliantly inventive writer whose books fell into a category sometimes called “slipstream”, a creative mix of genres often characterised by darkness lightened with playful humour. His best-known novel, Falling Angel (1978), is a mix of hard-boiled detective fiction and horror, a metaphysical noir that became Alan Parker’s classic movie Angel Heart (1987). Two years earlier, Hjortsberg had written the screenplay for another dark fantasy by a British director, Ridley Scott’s cult movie Legend (1985).

There was a haphazard karma to Hjortsberg’s career, much of it linked to his friend and fellow writer Thomas McGuane, whom he met at Yale drama school. Hjortsberg was born in New York City, where his father, Helge, a Swedish merchant sailor who had jumped ship there, ran a successful Swedish restaurant. When he was 10, his father died, and the family lost everything. His Swiss-born mother, Ida, worked as a maid to support the family. Hjortsberg took a degree in English at Dartmouth College in New Hampshire, and worked nights in a pizza parlour.

Facebook Twitter Pinterest William Hjortsberg with his daughter, Lorca, in 1969. Photograph: Bob Peterson/The Life Images Collection/Getty Images

He started at Yale in 1962, but then he and McGuane both won fellowships at Stanford University; before starting the programme Hjortsberg and his wife, Marian, travelled round Europe and central America. Hjortsberg wrote two novels, which were rejected widely, and was working as a shelf-stacker in Bolinas, California, when McGuane stepped in to help. He paid the copying and postage costs to send one of Hjortsberg’s manuscripts to his own editor at Simon & Schuster. A comedy set in a fairytale Switzerland, it was published as Alp in 1969, and followed by Gray Matters (1971), a brilliant piece of what was becoming known as speculative fiction, set in a post-apocalyptic world where only the brains of a select few had survived.

He wrote for magazines including Sports Illustrated and sold stories to Playboy, from which he won a “best new writer” prize. Symbiography, rejected by Playboy, was eventually published in Penthouse and in 1973 became a small-press novel and then an unproduced screenplay, Nomad. His fourth novel, Toro! Toro! Toro! (1974), was a comic take on bullfighting.

By now Hjortsberg had followed McGuane to live in Livingston, Montana, where he became the centre of the so-called Montana Gang, which included the writers Jim Harrison, Tim Cahill and Richard Brautigan, as well as the musician Jimmy Buffett and the actors Peter Fonda and Warren Oates. McGuane was by now an in-demand screenwriter, and advised Hjortsberg to try it.

When Hjortsberg pitched him an idea inspired by Stephen Vincent Benét’s short story The Devil and Daniel Webster, McGuane said: “That’s too good for Hollywood, write it as a novel.” Before publication, Falling Angel was serialised in Playboy and won the magazine’s annual best major work award. In the novel, set in 1959, New York private eye Harry Angel is hired by the mysterious Louis Cyphre to find Johnny Favorite, who disappeared after being wounded in a Nazi air raid in Tunisia in 1943. What starts as a Raymond Chandler-like detective story soon melds tropes of supernatural horror into a feverish tale of murder, voodoo and incest.

Hjortsberg would not publish another novel for many years. His only film credits came on screenplays for the B-movie king Roger Corman, but when Ridley Scott had the idea for a movie version of Tristan and Isolde, which became Legend, and came across some of Hjortsberg’s unproduced low-budget fare, he sought him out for the job.

Facebook Twitter Pinterest Tom Cruise and Mia Sara in the 1985 Ridley Scott film Legend, for which Hjortsberg wrote the screenplay. Photograph: Allstar/20th Century Fox

In 1994 Hjortsberg published Nevermore, a mystery starring Harry Houdini, Arthur Conan Doyle and the ghost of Edgar Allan Poe. By then he had begun work on a biography of Brautigan, which would take 20 years to research and write; Hjortsberg joked that Brautigan was laughing at him from beyond: “that poor old Hjortsberg spent years chasing me down”. Jubilee Hitchhiker was finally published in 2012.

A new novel, Mañana (2015), drawing on his experiences in Mexico in the 60s, is a tale of hippies, drugs and murder. He finished a long-awaited sequel to Falling Angel, which is due to be published later this year.

Hjortsberg’s first marriage, and a brief second marriage, ended in divorce. He is survived by his third wife, the painter Janie Camp, whom he married in 2007, and a daughter, Lorca, and son, Max, from his first marriage.

• William Reinhold Hjortsberg, writer, born 23 February 1941; died 22 April 2017