happens when driving and even if have car on but not moving it does
It seems unlikely that if the noise is even produced when the car is in motion, you'd be able to narrow it down to something inside of the engine compartment.
It could be a U-joint (universal joint) with a bad bearings. If it increases
with RPMs, and seems to come from the underside of the car, it could be the joint where the driveshaft connects to the tranmission, or one of the other U-joins where the driveshaft connects to the next segment of shaft.
There's an X-shaped joint inside of there. each of the four sides is packed with ball bearings and grease. Over time, that grease can escape, the bearings can seize, and the joint can bounce around amongst the remaining bearings resulting in a clicking sound on each rotation. If this is the case, you should be able to observe the movement happening.
To test out the u-joints, put the car in neutral. Grab ahold of the drive shaft at the joint, and turn it. There should be no play in the linkage; the shaft should rotate smoothly without moving back and forth inside of the bearing. If there is play, the u-joint needs to be replaced.