“He was like, ‘You know, she’s already mad at me, we was arguing, so I’ll be home later on tonight to make it up to her,’” Mr. Mendez, his friend, said.

Mr. Perez sometimes joked about how he was old enough to be his friends’ father. But their youth also gave him an escape, and he trusted them easily.

Around 2 a.m., after waffling a bit over going to a music studio for the night, Mr. Perez settled on driving Mr. Mendez and his girlfriend home and then facing his partner at their new apartment on Union Avenue, in a pocket of the 40th Precinct that was quieter than his favorite corner, but still only a short trip away.

Mr. Perez was “drunk talking” as he drove, Mr. Mendez recalled, and he urged Mr. Mendez’s girlfriend “to take care of him tonight.” He peeked at the side mirrors of his white Nissan Altima, as he often did, looking for the police or rivals.

After dropping them off, surveillance video showed Mr. Perez zigzagging through the maze of streets around his apartment building, parking and then grabbing a bag of Doritos from the car. There was a bounce in his step as he neared his building door.

Two gunmen waited about a dozen steps away, crouched behind several parked S.U.V.s. Two other assailants were positioned at the other end of the block. The ones closer to him slinked between the cars and the green plywood boards of a construction site.

Then one of the gunmen, slightly built and quick, lifted a .40-caliber pistol chest-high and squeezed off seven shots, shuffling like a boxer as he fired. He and the other gunman, who was trailing behind, took off running for a maroon van, got in and drove off.