In August 2009, with shooting about to start on The Beaver, Kyle Killen wrote a letter to the foetus in his wife's womb. He'd sold the screenplay, his first, only a few months earlier, and with Jodie Foster directing and Mel Gibson starring, his life was about to change completely. "The important thing to remember," Killen wrote on his blog, "will be that no matter how it turns out and no matter what people say or spray-paint on our house, it will be the reason you have a room to sleep in."

He didn't know the half of it. Killen didn't guess, for example, that the A-list star his little indie script had snagged would suddenly become one of the most vilified men in Hollywood. But he was getting used to hiccups. At the end of 2008, The Beaver was set to go into production with Steve Carell starring and Meet The Parents' Jay Roach directing. But things change, and they change, and then they change again. "From back when it seemed like it was going to be really simple," says Killen, who was raised and still lives in Texas, "the minute you decided you knew where this thing was going, it thwarted your expectations. After having that happen two or three times, by this point we were all, 'Er … I eagerly await the next chapter in this story, because it's useless to predict what's going to happen next.' Because it just seems to find new ways to go up and down to dizzying and absurd levels."

The Beaver started life modestly, first as a short story, and then an unpublished novel, in which a character who chronicles and touches his bowel movements gets so attached to one that he names it Barry and carries it around in a Tupperware box. Then Killen's wife became pregnant with twins, and he adapted it into a screenplay as a last-ditch attempt to keep his agent. "I had no expectations whatsoever that anything fantastical would happen," he says. "It was really just a signifier that I was still alive and still working. That was the limit of my expectations."

Barry the poo didn't make it into the screenplay, which by this point was focused solely on a depressed man who finds salvation through a discarded beaver puppet. Killen turned it in to his agent in May 2008. His wife gave birth a few days later, and a week after that, The Beaver was sold. Steve Golin, producer of Being John Malkovich and Eternal Sunshine Of The Spotless Mind had taken the film on; he then got Carell and Roach on board. They left after Carell's commitments to The Office clashed with the schedule, but like was attracting like; Jim Carrey expressed an interest, as did The Fighter director David O Russell. Everyone, it seemed, was reading Killen's script, which, by that December, had found itself at the top of The Blacklist, Hollywood's annual chart of the hottest unproduced screenplays. Killen wrote on his blog that the only lists he thought he'd top "would be ones in gas station restrooms signifying who was responsible for their last cleaning".

The Blacklist attracted the attention of Jodie Foster, who "came in and made a really strong pitch for how she would approach the film," says Killen. "She also said, 'I think this may be crazy, but I think a really amazing choice might be Mel Gibson.' We were really floored by that idea, and also sceptical." There were reasons for the scepticism. Gibson hadn't been the marquee name on a film since 2002's Signs, and as far as the public were concerned, his last starring role had been as himself in 2006, yelling drunken antisemitic slurs at cops. But Gibson, who had been close to Foster since they starred in 1994's Maverick, liked the script and signed up. Foster cast herself as his wife. The 43-day shoot found Gibson in good spirits; and although Foster says she occasionally had to reign him in from acting comedically, he fully intended to play it dark. "A little darker than some people had anticipated," she says.

Much has been made of the similarities between Gibson and The Beaver's depressed, ostracised, absurd Walter Black, with many interpreting his performance as a mea culpa for last year's leaked conversations with his ex-girlfriend in which Gibson, among other repulsive remarks insisted "I'll burn the goddamn house down but blow me first." Although the film was shot before that unpleasantness appeared online, and the parallels are somewhat superficial, there are similarities nonetheless. "He was so pathetic, this guy," Gibson told Deadline Hollywood in April. "I understood that aspect of him. Every aspect of it: the drinking, the obsessive-compulsive kind of stuff, and that he's tried everything. Oh yeah."

Foster believes Gibson's experiences contributed to his understanding of the character: "You obviously have conversations about what it's like to fall apart in a hotel room and be at the lowest stage of your life," she has said, "he brings a lot to the table there."

'Even if people are trashing it, there's a little bit of honour in being out there enough to be trashed. There are a lot of things that don't make it that far' – Kyle Killen

He also brought to the table some heavyweight directorial experience, which came in handy when his head was inadvertently smashed open during filming. Gibson was required to crack a prop lamp over his own head. "He did the take, and it appeared to hurt him and be painful," says Killen, "but that was just what it was supposed to look like. It was only when it was over and he was really upset and bleeding that you saw something had gone wrong." The prop department hadn't fully prepared the lamp to break on impact, and blood was gushing everywhere. There was no medic; Gibson was shouting while Foster raced to get a first-aid kit. Gibson collected himself, then showed the prop department how to prepare the lamp for another take. "He deals with this sort of stuff all the time in action films," says Killen, "and he immediately knew what should have been done. It was a fascinating little glimpse into what he's got in there."

The world got an even more fascinating glimpse of what he's got in there a few months later when, on the morning of the final day of reshoots, Gibson's bilious rants to his ex-girlfriend surfaced. "It wasn't fun," said Foster recently, describing how his assistant called her to "Come to the trailer!", where the star was "a mess". He then shot the scene, gave Foster a hug, got on a plane and left. And suddenly, the film was in limbo again. "I think there were a lot of people saying, 'Hmm. I'm not yet sure what this means or what any of us should do,'" says Killen, who simply says it was "a fascinating development". (It appears Killen was by now something of a Zen master, watching from a discreet distance as the curveballs got weirder.) The film's fate was out of his hands; for the time being, it was out of everyone's hands.

In an attempt to limit the damage caused by the further plummet in Gibson's reputation, The Beaver saw its release delayed not once, but three times. Finally, almost a year later, it secured an American opening date, in May. Foster said the final few months were "difficult", admitting that for quite some time the perception was that there had been no audience for the film. When opening weekend came, it seemed there still wasn't an audience. Although it had a minor release, opening on 22 screens, it made relatively little money and was quickly deemed a box office flop by websites reporting its failure with a vigour equal to their enthusiasm for the script when it topped The Blacklist.

Americans may well have avoided The Beaver because of Gibson, or because of the middling reviews, or because they just didn't want to see a film about a depressed man and a beaver puppet. Its UK fortunes will be decided when it is released here on Friday. Foster, meanwhile, promoting the film without the aid of her co-star, has been defending him as much as possible. "People say nasty things to cops when they're drunk," she said to one reporter. It's hard, however, to imagine Foster calling a policewoman "sugar tits".

One idea Foster contributed to The Beaver was the not-entirely-original metaphor of life as a rollercoaster: ups and downs, highs and lows etc. It's certainly apt in terms of the development of Killen's screenplay, and the last three years of his life. He credits The Beaver with helping to get his television series Lone Star commissioned last year (he also claims to be at peace with the fact that it was cancelled by Fox after two episodes). And mere days after The Beaver's rather dismal opening weekend, a new Killen-created series, Awake, starring Jason Isaacs as a man living two separate realities, was commissioned by US network NBC. It is already being heralded as the next Lost. After that, he's writing a film for Working Title.

"I have the extraordinarily awesome problem of having more things to do than I have time for," he says after returning from Cannes, where, after The Beaver's glitzy, supermodel-filled screening ended with him, Foster and Gibson receiving a "surreal" 10-minute standing ovation, he decided to draw a line under the film.

"Even if [people] are trashing it, I guess I feel like there's a little bit of honour in being out there enough to be trashed," he says. "There are a lot of things that don't make it that far. That said, the day after the Cannes screening I deleted my column on Twitter that follows The Beaver. At this point I have no idea of what will happen to it, how it will fare and so on and so forth. I'm sure some people will continue to like it, some people will continue to hate it, and they will be correct. But I feel like we sort of had the perfect experience with it, and at this point I'm ready to let go of needing to know what happens to it. You kind of can't beat what we went through." Indeed.