In the new comedy “Brooklyn Nine-Nine,” Andy Samberg plays Jake Peralta, a gifted but lazy detective who has some growing up to do.

The former “Saturday Night Live” regular says when he was offered the show, the one thing he knew for sure was that he was going to get “to wear a cool leather jacket.” But he also thought it was important that his character be smart and not just a jerk who loves to clown around.

“I think it’s important for the show to work — because, otherwise, why do you care to track stories and stuff like that — if he’s not actually good at it?” Samberg said. “Then when he’s being kind of a jackass, you can forgive him more and be on board with him more. When we first talked about it, it was more like he was going to be kind of like McNulty from ‘The Wire,’ but instead of drinking problems and philandering, he was doing gags in the office.”

While Jake’s behavior is tolerated by his old commander because of his brilliant crime-solving abilities, when a new one arrives he is asked to toe the line, causing friction. The new by-the-book captain, Ray Holt, is played by Andre Braugher, who spent years as a detective on the gritty police drama “Homicide: Life on the Street.”

“They’re a funny group, all of them, from beginning to end,” Braugher said about the “Brooklyn” cast. “You know, there’s a different spirit with a comedy. So there’s a little bit of a learning curve from the pilot to the first couple of episodes. But I’m watching these guys like hawks. So I feel like I’m getting on board and exploring a new way that works.”

The creators of “Brooklyn Nine-Nine” aren’t making “Barney Miller” though, which was centered around the squad room. In their show, there is action, even gunplay, and there are bad guys to be caught.

“It’s always about finding the line,” said Dan Goor, one of the executive producers. “If the crime is too silly in the first place, it’s very hard to write a compelling story because the audience just checks out. They don’t care if you solve the crime.”